0

I'm yet to understand why I'm getting 'HttpResponse' error.

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.

This view is responsible for getting a POST request from an API and load the data and do things with it.

Views:

@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,])

What am I missing here?

Arpit Solanki
  • 9,567
  • 3
  • 41
  • 57
smack
  • 922
  • 11
  • 21

1 Answers1

1

You need to return an HttpResponse on every condition.In last if else statement you can see you are not returning anything from the view so you have to return an appropriate http response for every case in your view. See updated code below.

@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

Arpit Solanki
  • 9,567
  • 3
  • 41
  • 57
  • I did that earlier but the thing is the view won't go through the lines.. it will stop at the last else statement and return the HttpResponse('no data') when the API has correct data. – smack Jan 17 '18 at 08:30
  • There is an if-elif statement combo in your code if any of them does not get true then your view won't return an httpresponse. see the updated answer and comments in the code – Arpit Solanki Jan 17 '18 at 08:35
  • Thanks so much Arpit. Unfortunately, I will have to close this due to SO rules. Your solution is effective. – smack Jan 17 '18 at 08:54
  • There is no rule that you have to close this. If the answer helped you then upvote and accept the answer – Arpit Solanki Jan 17 '18 at 08:57
  • Some folks on here have voted for it to be closed and it's showing duplicate answer. I will do that and leave it for SO to take action. – smack Jan 17 '18 at 08:58