【问题标题】:'str' object has no attribute 'csrf_exempt' while returning response from a django view?“str”对象在从 django 视图返回响应时没有属性“csrf_exempt”?
【发布时间】:2013-02-21 03:28:49
【问题描述】:

我的视图有一个奇怪的问题,它以 json 格式返回响应。我的视图如下所示:

def CheckPlayer(request,client_id):
    if request.method == 'GET':
        try:
            user = User.objects.get(id = client_id)
        except:
            return Error(message = "User doesnot exists.")
        message = request.GET.get('message','')
        if not message:
            return Error(message = "Argument Missing.")
        response = {}
        result = MakingRequest(message)
        result = json.loads(result)
        if result['failure'] == '0':
            response['failure'] = '0'
        else:
            response['failure'] = '1'
        return HttpResponse(json.dumps(response), mimetype="application/javascript")
    else:
        return Error()


def MakingRequest(message):
    values = {'message':message}
    rObjects =  Ram.objects.all()
    temp = []
    for i in rObjects:
        temp.append(i.appId)
    values['registration_ids'] = temp
    param = json.dumps(values)
    req = urllib2.Request("https://android.googleapis.com/gcm/send", param)
    req.add_header( 'Content-Type' , 'application/json' )
    req.add_header( 'Authorization' , 'key=7FcEMnl0FRTSBjhfjfhjfHi1Rmg04Ns' )
    response = urllib2.urlopen(req)
    return response.read()

我已经在我的本地服务器上对其进行了测试,它运行良好,但是如果我在我的服务器(nginx、gunicorn、django-mongoDB、mongoDB)上运行它,那么它会给我这个错误。我知道这个错误,如果一个视图不从视图返回 HttpResponse 然后它 djangi 引发错误 "Nonetype object has no attribute csrf_exempt' " 但在我的情况下,我返回的是 json 格式的响应,但它仍然给我错误。请帮助我

【问题讨论】:

  • 不确定 Error() 返回什么。您的产品可能会返回您的错误()案例之一,它可能会返回一个字符串,所以这可能是一个问题
  • 但是为什么当我没有在这个视图中使用 csrf_exempt 装饰器时甚至会调用它

标签: python django django-nonrel gunicorn


【解决方案1】:

当您返回 Error() 时,您没有返回 HttpResponse,因此 Django 无法将装饰器应用于它。

【讨论】:

    猜你喜欢
    • 2015-11-01
    • 1970-01-01
    • 2012-11-05
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    相关资源
    最近更新 更多