Categories
Python Answers

How to call a view from within another view with Python Django?

Spread the love

Sometimes, we want to call a view from within another view with Python Django.

In this article, we’ll look at how to call a view from within another view with Python Django.

How to call a view from within another view with Python Django?

To call a view from within another view with Python Django, we can call the view function directly.

For instance, we write

def view1(request):
    # ...
    return HttpResponse("some html here")

def view2(request):
    response = view1(request)
    # ...
    return HttpResponse("some different html here")

to call view1 in view2.

And then we can get the response returned and do what we want with it before we return the response.

We can also return the response directly in view2`.

Conclusion

To call a view from within another view with Python Django, we can call the view function directly.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *