0

I am new for Django1.10 Python.

I've to provide web services for an app. So I created a web service in python's Django framework. The web service is properly working for iOS but getting issue while handling with android. For dealing with web services in android, using Volley library.

Following error occurring:- Error Code 500 Internal Server Error

So unable to POST data through android end...

For Web service I am using following code :-

views.py

from django.http import HttpResponseRedirect, HttpResponse
from django.views.decorators import csrf
from django.views.decorators.csrf import csrf_protect, csrf_exempt
from django.db import IntegrityError, connection
from django.views.decorators.cache import cache_control
from django.core.files.images import get_image_dimensions                
import json
from json import loads, dump
from models import Gym


@csrf_exempt
def gym_register_web(request):

    data = json.loads(request.body)
    gN = str(data['gym_name'])
    gPh = str(data['gym_phone'])
    gE = str(data['gym_email'])
    gL = str(data['gym_landmark'])
    gAdd = str(data['gym_address'])

    exE = Gym.objects.filter(gym_email = gE)

    if exE:
        status = 'failed'
        msg = 'EmailId already exist'
        responseMsg = '{\n "status" : "'+status+'",\n "msg" : "'+msg+'"\n}'
        return HttpResponse(responseMsg)
    else:
        gymI = Gym(gym_name = gN, gym_phone = gPh, gym_email = gE, gym_area = gL, gym_address = gAdd)
        gymI.save()
        data = Gym.objects.get(gym_email = gE)
        status = 'success'
        dataType = 'gym_id'
        val = str(data.gym_id)
        responseMsg = '{\n "status" : "'+status+'",\n "'+dataType+'" : "'+val+'"\n}'
        return HttpResponse(responseMsg)

urls.py

from django.conf.urls import url, include
from . import views
from django.views.decorators.csrf import csrf_protect, csrf_exempt
admin.autodiscover()

urlpatterns=[
    url(r'^gymRegister/$', views.gym_register_web),
    ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

EDIT: models.py

from django.db import models
from django.contrib import admin

class Gym(models.Model):
    gym_id = models.AutoField(primary_key = True)
    gym_name = models.CharField(max_length = 100, null=True,default = None )
    gym_email = models.CharField(max_length = 100, null=True,default = None )
    gym_phone = models.BigIntegerField(null=True,default = None )
    gym_area = models.TextField(max_length = 255, null=True,default = None )
    gym_address = models.TextField(max_length = 255, null=True,default = None )
    gym_latitude = models.CharField(max_length = 100, null=True,default = None )
    gym_longitude = models.CharField(max_length = 100, null=True,default = None )
    gym_status = models.IntegerField(null=True,default = None )
    gym_website = models.CharField(max_length = 255,null=True,default = None )
    gym_ladies_special = models.IntegerField(null=True,default = None )

I tested web service on Advance REST Client providing desired output, and I would like to remind once again the web service is properly working for iOS

So, Where Should I improve my code...?

Thanks in Advance :)

EDIT:

my android developer trying to send data in json object manner

{ "key1"="val1", "key2"="val2"}

instead sending it in json array(key:value) manner

{
    "key1" : "val1",
    "key2" : "val2"
}

How can I fetch data sent in object format...

ThankYou

1 Answers1

0
  1. Make sure that in your 'request.body', you are getting a proper dictionary which have all the keys u accessed below.Because,if your 'data' variable dont have any key that you accessed below, it will raise keyerror and u didnt handle keyerror in your code.

If above does not worked, then show me your models.py

Akash Wankhede
  • 618
  • 6
  • 15
  • I added `models.py` file...thanks...and I already mentioned that same code is working for `iOS`, and also gives proper response on `ARC` only issue with android...@Akas – Shreejay Pendse Oct 14 '16 at 12:51
  • Models seems okay. iOS, ARC and Android clients are three different things. All clients are sending you data in 'request.body'. So, I am saying that if you can print your 'request.body, in your view on your local server, then you will get to know that whether data in request.body is getting properly or somethig missing from android. – Akash Wankhede Oct 14 '16 at 13:03
  • when my colleague tried to post data, he got `Internal Server Error 500` – Shreejay Pendse Oct 14 '16 at 13:13
  • So, you will have to send data from android side exactly same like you are sending from iOS and ARC. You may upvote my answer i this works for you. Thanks. – Akash Wankhede Oct 14 '16 at 13:13
  • From android client, you require dictionary whose structure will be something like this {'gym_name':value1,'gym_phone':value2,'gym_email':value3,'gym_landmark':value4,'gym_address':value5} – Akash Wankhede Oct 14 '16 at 13:17
  • Thanks Bro..I tried to post exact data but getting same error...Have you know any other method for fetching data from server instead `data = json.loads(request.body)`...at andorid side developer using POST method..I upvoted your answer but due my lower reoutation it won't be shown... – Shreejay Pendse Oct 14 '16 at 13:19
  • actually it is conventional and easy way to send data in request.body. But if you want other way then you can send it through POST parameter through android, which you can get by doing 'request.POST.get('key_name')',but for this you will have to write another view or modify current view. – Akash Wankhede Oct 14 '16 at 13:25
  • can you help me how to fetch posted data in `json-object` format – Shreejay Pendse Oct 15 '16 at 07:38
  • I can help you with python and django.I dont know enough about android.. – Akash Wankhede Oct 15 '16 at 07:43