【发布时间】:2014-04-20 16:47:11
【问题描述】:
我是 json、Python 和 Django 的新手。我在网上做了一些研究,但没有一个能解决我的问题。提前感谢您提供任何见解!
我正在构建一个允许移动设备更新服务器数据库的系统,该数据库由 Django 管理。我目前只在我向 Django 识别的 URL 发送请求的本地机器上进行测试。
第一步我有一段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"])
【问题讨论】:
标签: python json django rest serialization