0

我是 Django1.10 Python 的新手。

我必须为应用程序提供网络服务。所以我在 python 的 Django 框架中创建了一个 web 服务。Web 服务正常工作,iOS但在处理android. 用于处理 android 中的 Web 服务,使用Volley库。

发生以下错误:- 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

那么,我应该在哪里改进我的代码......?

提前致谢 :)

编辑:

我的 android 开发人员试图以 json 对象方式发送数据

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

而是以 json array(key:value) 方式发送它

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

如何获取以object格式发送的数据...

谢谢你

4

1 回答 1

0
  1. 确保在您的“request.body”中,您获得了一个正确的字典,其中包含您在下面访问的所有键。因为,如果您的“数据”变量没有您在下面访问的任何键,它将引发 keyerror 而您没有处理代码中的 keyerror。

如果上述方法不起作用,请向我展示您的 models.py

于 2016-10-14T12:36:12.027 回答