值错误:视图没有返回HttpResponse对象。它返回了None而不是Django

时间:2018-01-17 08:05:12

标签: python django

我还没理解为什么我会得到' 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,])

我在这里缺少什么?

1 个答案:

答案 0 :(得分: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