I am new for Django1.10 Python
我必须为一个应用程序提供web服务。所以我在python的Django框架中创建了一个web服务。web服务在iOS
中正常工作,但在处理android
时遇到问题。在android中使用Volley
库来处理web服务。
出现以下错误:-Error Code 500 Internal Server Error
所以无法通过android端发送数据…
对于Web服务,我使用以下代码:- 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)
编辑: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 )
我在Advance REST Client
上测试了web服务,提供了所需的输出,我想再次提醒web服务是正常工作的iOS
那么,我应该在哪里改进我的代码…?
Thanks in Advance:)
编辑:我的android开发人员试图以json对象方式发送数据
{ "key1"="val1", "key2"="val2"}
而不是以json array(key:value)方式发送
{
"key1" : "val1",
"key2" : "val2"
}
如何获取object
格式发送的数据…
谢谢
- 请确保在您的请求。Body ',你会得到一个合适的字典,里面有你在下面访问过的所有键。因为,如果你的'data'变量没有任何你在下面访问的键,它会引发keyerror,而你没有在你的代码中处理keyerror。
如果上面没有工作,然后显示你的models.py