Redirecting URLs in Django

Redirecting using HttpResponseRedirect

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

from django.http import HttpResponseRedirect

Before we any demonstrate how to use HttpResponseRedirect visit http://127.0.0.1:8000/blog/, 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 urls.py in the blog app and add the following url pattern at the beginning of urlpatterns list.

urlpatterns = [
    url(r'^blog/$', views.test_redirect, name='test_redirect'),
    ...
]

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

from django.http import HttpResponse, HttpResponseNotFound, HttpResponseRedirect

...

def test_redirect(request):
    return HttpResponseRedirect("/")

Visit http://127.0.0.1:8000/blog/ and HttpResponseRedirect will redirect you to http://127.0.0.1:8000/. We can also specify any external URL like http://example.com/ in which case visiting http://127.0.0.1:8000/blog/ will redirect the user to http://example.com/.

def test_redirect(request):
    return HttpResponseRedirect("http://example.com/")

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

from django.urls import reverse

def test_redirect(request):
    return HttpResponseRedirect(reverse('post_list'))

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.

from django.http import HttpResponsePermanentRedirect

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

def test_redirect(request):
    return HttpResponsePermanentRedirect(reverse('post_list'))

Visit http://127.0.0.1:8000/blog/ again, this time you will be redirected to http://127.0.0.1:8000/ using permanent redirect (i.e HTTP 301 redirect).

redirect() shortcut

redirect() is another common method Django provides 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.

from django.shortcuts import redirect

Open views.py and amend test_redirect() view to use redirect() instead of HttpResponsePermanentRedirect.

from django.shortcuts import render, get_object_or_404, get_list_or_404, redirect

...

def test_redirect(request):
    return redirect('post_list')

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

def test_redirect(request):
    return redirect('post_list', 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 views.py again and modify test_redirect() view as follows:

def test_redirect(request):
    c = models.Category.objects.get(name='python');
    return redirect(c)

Trying to pass a Model object to redirect() without defining get_absolute_url() in the Model class will result in an error.

[show_error]

Visit http://127.0.0.1:8000/blog/ and test_redirect() method will redirect you to http://127.0.0.1:8000/category/python/.