I am using tastypie. When I want to post data to my server I am getting:
"global name 'http' is not defined"
Error message.
My curl command is
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/
Traceback:
{"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"}
I put the address in quotes, but nothing has changed.
Also I wrote this to my resource:
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))
What is the problem?
Your exception handler in that resource expects http
to be in the local namespace:
raise ImmediateHttpResponse(response=http.HttpBadRequest(e.message))
If you don't have something like from tastypie import http
earlier in your code, this will fail.
As an aside, for small things like this I generally prefer to use something like from tastypie.http import HttpBadRequest
to catch incorrect names immediately using a tool like pyflakes or flake8. If the only reference to HttpBadRequest
was in infrequently-executed code like that except
block it can take awhile before you notice a problem like this.