2

I'm trying to call a function into a function in django but I keep getting:

The view app.views.function1 didn't return an HttpResponse object. It returned None instead.

My views are:

def function1(request):
    [some api calls] 
    #Once this process is done I want to call my second function
    function2()

and then I have

def function2(request):

How you call an other function in Django/Python easily

p.s. These functions could just be one, I just want to divide them to have my code more readable and have one function do only one thing.

2 Answers 2

7

The error is pretty clear:

The view app.views.function1 didn't return an HttpResponse object. It returned None instead.

You need to return an HttpResponse object when finishing function1, so if function2 is your last function, it should return the HttpResponse object and you should return the result of that function as well:

def function1(request):
    [some api calls] 
    #Once this process is done I want to call my second function
    return function2(request)

def function2(request):
    # some hard work
    return HttpResponse(...)
Sign up to request clarification or add additional context in comments.

Comments

1

Generally, when I have a utility function, I factor out the utility portion so that multiple views can reference the same utility function without needing to worry about the request object / context.

But more specifically, your first function (function1) is not returning an HttpResponse object (As the error describes).

You have two paths to go down:

  1. Have func2 return an HttpResponse object (And pass it the request argument as well) and then have func1 return what func2 returned (an HttpResponse object).

  2. Have func2 return whatever it is you want, and have func1 return an HTTPResponse object.

In either case, func1 must return an HttpResponse object.

However, like I said above, if both func 1 and func 2 are valid views with urls and all that jazz, I would factor out whatever it is you want in func2 out to another function entirely and have both func1 and func2 reference that (in general).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.