我对json、Python和Django还不熟悉。我在网上做了一些研究,但都没有解决我的问题。提前感谢您的任何见解!
我正在构建一个系统,允许移动设备更新由Django管理的服务器数据库。我目前只在本地机器上进行测试,在那里我向url Django识别器发送请求。
第一步,我有一段python代码,试图与服务器进行通信。
# in test.py:
data = '''{"pk": 4, "model": "arts"}'''
data = json.loads(data)
data = json.dumps(data)
URL = "my local host's URL"
h = httplib2.Http(".cache")
resp, content = h.request(URL, "POST", body = data)然后在服务器上调用视图函数。
# in views.py:
def Updates(request, category):
if request.method=='POST':
print 'Data: %s' % request.body
## this prints successfully:
## > Data: {"pk": "4", "model": "arts"}
resultJson = serializers.deserialize('json', request.body)
for obj in resultJson:
print "OK"
return HttpResponse(request.body)
else:
return HttpResponse("Wrong Method")我得到的错误消息是:
Django Version: 1.6.2
Exception Type: DeserializationError
Exception Value: string indices must be integers
...
Traceback Switch to copy-and-paste view
C:\Python27\lib\site-packages\django\core\handlers\base.py in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
C:\Python27\lib\site-packages\django\views\decorators\csrf.py in wrapped_view
return view_func(*args, **kwargs)
C:\pathToViewsFile\views.py in Updates
for obj in resultJson:
C:\Python27\lib\site-packages\django\core\serializers\json.py in Deserializer
six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2])
C:\Python27\lib\site-packages\django\core\serializers\json.py in Deserializer
for obj in PythonDeserializer(objects, **options):
C:\Python27\lib\site-packages\django\core\serializers\python.py in Deserializer
Model = _get_model(d["model"])https://stackoverflow.com/questions/22421181
复制相似问题