【问题标题】:Why I am getting global name 'http' is not defined error?为什么我得到全局名称“http”未定义错误?
【发布时间】:2014-09-12 00:20:27
【问题描述】:

我正在使用美味派。当我想将数据发布到我的服务器时,我得到:

“未定义全局名称'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))

有什么问题?

【问题讨论】:

  • 把地址放在引号里。
  • 请再次检查我的问题。

标签: django tastypie


【解决方案1】:

您在该资源中的异常处理程序要求 http 位于本地命名空间中:

raise ImmediateHttpResponse(response=http.HttpBadRequest(e.message))

如果您的代码前面没有 from tastypie import http 之类的东西,这将失败。

顺便说一句,对于像这样的小事情,我通常更喜欢使用from tastypie.http import HttpBadRequest 之类的东西,使用pyflakesflake8 之类的工具立即捕获不正确的名称。如果对HttpBadRequest 的唯一引用是在像except 块这样的不经常执行的代码中,则可能需要一段时间才能注意到这样的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多