【问题标题】:Value error: view didn't return an HttpResponse object. It returned None instead Django值错误:视图未返回 HttpResponse 对象。它返回 None 而不是 Django
【发布时间】:2018-01-17 17:00:16
【问题描述】:

我还不明白为什么会收到“HttpResponse”错误。

Traceback (most recent call last):
  File "C:\Python27\Scripts\covaenv\lib\site-packages\django\core\handlers\exception.py", line 42, in inner
    response = get_response(request)
  File "C:\Python27\Scripts\covaenv\lib\site-packages\django\core\handlers\base.py", line 198, in _get_response
    "returned None instead." % (callback.__module__, view_name)
ValueError: The view exampleapp.views.get_recieve_update didn't return an HttpResponse object. It returned None instead.

此视图负责从 API 获取 POST 请求并加载数据并对其进行处理。

观看次数:

@csrf_exempt
def get_recieve_update(request):
    if request.method=="POST":
        man= json.loads(request.body)
        txId = man['hash']
        uri = bgo_main_base_url + '/wallet/{}/tx/{}'.format(WALLETID, txId)
        rexa = requests.get(uri, headers=headers)
        vd = rexa.json()
        isMine = vd['outputs'][0]['isMine']
        confirmations = vd['confirmations']
        if isMine == True and confirmations > 1:
            address = vd['outputs'][0]['account']
            value = vd['outputs'][0]['value']
            try:
                get_adr = CPro.objects.get(address = address)
            except CPro.DoesNotExist:
                get_adr = None

            if not get_adr.is_used==True and get_adr.is_active==False:
                update_cw = CW.objects.filter(user = 
     get_adr.user).update(current_btc_balance=F('current_btc_balance') + value , modified_date=datetime.datetime.now())
                return HttpResponse('done')

            elif get_adr.is_used==True and get_adr.is_active==False:
                address = vd['outputs'][0]['account']
                value = vd['outputs'][0]['value']
                send_mail('Recieved on Used Address','failed to credit for {} with {} and id {}'.format(address, value, txId), DEFAULT_FROM_EMAIL,[DE_MAIL,])
        else:
            address = vd['outputs'][0]['account']
            value = vd['outputs'][0]['value']
            send_mail('Recieved Callback Error','failed to credit for {} with {}'.format(address, value), DEFAULT_FROM_EMAIL,[DE_MAIL,])

我在这里错过了什么?

【问题讨论】:

  • 除了一个特定的 if 语句之外,您的视图没有返回任何内容。
  • 你是什么意思?你能详细说明一下吗?
  • 即如果isMine 为假...则不会返回任何内容。
  • 看到 webhook 会在 isMine 为 True 时调用 url。在这种情况下,这也是 True,我只是把那行放在那里确定。
  • 你的意思是如果它是假的,我应该写另一个声明?

标签: python django


【解决方案1】:

您需要在每个条件下返回HttpResponse。在最后一个 if else 语句中,您可以看到您没有从视图中返回任何内容,因此您必须为视图中的每个案例返回适当的 http 响应。请参阅下面的更新代码。

@csrf_exempt
def get_recieve_update(request):
    if request.method=="POST":
        man= json.loads(request.body)
        txId = man['hash']
        uri = bgo_main_base_url + '/wallet/{}/tx/{}'.format(WALLETID, txId)
        rexa = requests.get(uri, headers=headers)
        vd = rexa.json()
        isMine = vd['outputs'][0]['isMine']
        confirmations = vd['confirmations']
        if isMine == True and confirmations > 1:
            address = vd['outputs'][0]['account']
            value = vd['outputs'][0]['value']
            try:
                get_adr = CPro.objects.get(address = address)
            except CPro.DoesNotExist:
                get_adr = None

            if not get_adr.is_used==True and get_adr.is_active==False:
                update_cw = CW.objects.filter(user = 
     get_adr.user).update(current_btc_balance=F('current_btc_balance') + value , modified_date=datetime.datetime.now())
                return HttpResponse('done')

            elif get_adr.is_used==True and get_adr.is_active==False:
                address = vd['outputs'][0]['account']
                value = vd['outputs'][0]['value']
                send_mail('Recieved on Used Address','failed to credit for {} with {} and id {}'.format(address, value, txId), DEFAULT_FROM_EMAIL,[DE_MAIL,])
                return HttpResponse("Some appropriate response")
            else:
                # return something. If both condition from does not get true then there will be no return from view
        else:
            address = vd['outputs'][0]['account']
            value = vd['outputs'][0]['value']
            send_mail('Recieved Callback Error','failed to credit for {} with {}'.format(address, value), DEFAULT_FROM_EMAIL,[DE_MAIL,])
            return HttpResponse("Some appropriate response") # <-- here you were not returning a response

Another Helpful answer

【讨论】:

  • 我之前做过,但问题是视图不会通过这些线。它会在最后一个 else 语句处停止并在 API 正确时返回 HttpResponse('no data')数据。
  • 您的代码中有一个 if-elif 语句组合,如果其中任何一个不为真,那么您的视图将不会返回 httpresponse。在代码中查看更新的答案和 cmets
  • 非常感谢 Arpit。不幸的是,由于 SO 规则,我将不得不关闭它。你的解决方案是有效的。
  • 没有规定你必须关闭它。如果答案对您有帮助,请投票并接受答案
  • 这里的一些人投票支持关闭它,它显示重复的答案。我会这样做并留给 SO 采取行动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-01
  • 2014-12-03
  • 2021-06-22
  • 2017-01-25
  • 2018-09-14
  • 1970-01-01
相关资源
最近更新 更多