我正在使用tastypie。当我想将数据发布到我的服务器时,我得到了:
“全局名称'http'未定义”
错误讯息。
我的curl命令是
curl --dump-header - -H "Content-Type: application/json" -X POST --data '{{"shop" : "/api/shop/1/","transactions" : [{"item" : "/api/item/53/","note" : "Normal"}]}' http://localhost:5000/api/order/
回溯:
{"error_message": "global name 'http' is not defined", "traceback": "Traceback (most recent call last):\n\n File \"/Users/burakkilic/Documents/Projects/FineDine/Server/Django/finedine/venv/lib/python2.7/site-packages/tastypie/resources.py\", line 195, in wrapper\n response = callback(request, *args, **kwargs)\n\n File \"/Users/burakkilic/Documents/Projects/FineDine/Server/Django/finedine/venv/lib/python2.7/site-packages/tastypie/resources.py\", line 426, in dispatch_list\n return self.dispatch('list', request, **kwargs)\n\n File \"/Users/burakkilic/Documents/Projects/FineDine/Server/Django/finedine/venv/lib/python2.7/site-packages/tastypie/resources.py\", line 458, in dispatch\n response = method(request, **kwargs)\n\n File \"/Users/burakkilic/Documents/Projects/FineDine/Server/Django/finedine/venv/lib/python2.7/site-packages/tastypie/resources.py\", line 1317, in post_list\n deserialized = self.deserialize(request, request.body, format=request.META.get('CONTENT_TYPE', 'application/json'))\n\n File \"/Users/burakkilic/Documents/Projects/FineDine/Server/Django/finedine/finedine/api.py\", line 203, in deserialize\n raise ImmediateHttpResponse(response=http.HttpBadRequest(e.message))\n\nNameError: global name 'http' is not defined\n"}
我把地址放在引号中,但没有任何改变。
我也把它写到我的资源中:
def deserialize(self, request, data, format='application/json'):
try:
return super(OrderResource, self).deserialize(request, data, format=format)
except Exception as e:
# if an exception occurred here it must be due to deserialization
raise ImmediateHttpResponse(response=http.HttpBadRequest(e.message))
有什么问题?
答案 0 :(得分:1)
该资源中的异常处理程序期望http
位于本地名称空间中:
raise ImmediateHttpResponse(response=http.HttpBadRequest(e.message))
如果您的代码中前面没有from tastypie import http
之类的内容,则会失败。
顺便说一下,对于像这样的小事,我通常更喜欢使用像from tastypie.http import HttpBadRequest
这样的东西,使用像pyflakes或flake8这样的工具立即捕捉错误的名字。如果对HttpBadRequest
的唯一引用是在except
块之类的不经常执行的代码中,那么在您发现此类问题之前可能需要一段时间。