Redirecting URLs in Django

Redirecting using HttpResponseRedirect

HttpResponseRedirect is a subclass of HttpResponse. As its name says, instead of returning a normal response, it redirects the user to a different URL using a temporary redirect (i.e HTTP 302 status code). It accepts a string path to redirect to. To use HttpResponseRedirect you have to import it from django.http module.

Before we any demonstrate how to use HttpResponseRedirect visit, you should see a 404 page as follows:

404 page

This happens because the /blog/ URL is not mapped to any view function.

Now open in the blog app and add the following URL pattern at the beginning of urlpatterns list.


In, add test_redirect() view function at the end of the file.


Visit and HttpResponseRedirect will redirect you to We can also specify any external URL like, in that case visiting will redirect the user to

Instead of manually typing the URLs, we can also use reverse() method with HttpResponseRedirect to automatically create urls. For example:


Redirecting using HttpResponsePermanentRedirect

Django provides another method called HttpResponsePermanentRedirect which works exactly like HttpResponseRedirect but performs a permanent redirect (i.e HTTP 301 redirect). To use this class first import it from django.http module.

Open file and amend test_redirect() to use HttpResponsePermanentRedirect as follows:


Visit again, this time you will be redirected to using a permanent redirect (i.e HTTP 301 redirect).

The redirect() shortcut

The redirect() is shortcut function to redirect the users to different URLs. It accepts URL path or name of the URL pattern to redirect to. To use it first import it from django.shortcuts module.

Open and amend test_redirect() view to use redirect() instead of


By default, the redirect() performs a temporary redirect i.e HTTP 302 redirect, to do a permanent redirect pass permanent=True.

Things become a little more interesting when you find out, you can also pass a Model object like, Post or Category to the redirect() function. If Model class has defined get_absolute_url() method then redirect() method will redirect the user to the URL returned by the get_absolute_url() method.

Open again and modify test_redirect() view as follows:


Visit and test_redirect() method will redirect you to

Trying to pass a Model object to redirect() without defining get_absolute_url() in the Model class will cause TypeError exception to be thrown (or 500 Internal Server Error in production).

Note: To checkout this version of the repository type git checkout 19a.

1 thought on “Redirecting URLs in Django

  1. Request Method: POST
    Request URL:
    Django Version: 2.1.5
    Exception Type: ValueError
    Exception Value:
    The view accounts.views.register didn’t return an HttpResponse object. It returned None instead.
    Exception Location: C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\ in _get_response, line 137
    Python Executable: C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\python.exe
    Python Version: 3.7.1
    Python Path:
    Server time: Fri, 15 Feb 2019 09:53:25 +0000

Leave a Comment

%d bloggers like this: