How To Use HttpResponseRedirect In Django

HttpResponseRedirect In Django

The HttpResponseRedirect function in Django is used for purposes of URL redirection or URL forwarding. Redirects can be used for many purposes like redirecting users during a maintenance window, providing more than one URL address to a web page on the internet, or for configuring permanent redirects in order to maintain links and bookmarks that already exist. Typically redirects are used with HTTP, HTTPS, and FTP protocols, and a status code of 302 is used to perform this operation. Let’s add an instance of HttpResponseRedirect in our Django app to see how it works.


Two URLs One Webpage

An example of URL redirection in our application could be offering the user to view daily, weekly, or monthly goals using those strings or as an option, integer values. So the goal is to have two URLs for each of the three pages like so.

daily goals
127.0.0.1:8000/goals/daily
127.0.0.1:8000/goals/1

weekly goals
127.0.0.1:8000/goals/weekly
127.0.0.1:8000/goals/2

daily goals
127.0.0.1:8000/goals/monthly
127.0.0.1:8000/goals/3

Two Route Options

The first step is to offer two different routes in the URLconf of urls.py. One route will match an integer-based timeframe, and the other will match a string-based timeframe.

C:\python\djangoprojects\myproject\goals\urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('<int:timeframe>', views.goals_by_int_timeframe),
    path('<str:timeframe>', views.goals_by_timeframe),
]

Importing HttpResponseRedirect

To make use of HttpResponseRedirect, we first need to import it at the top of view.py.

C:\python\djangoprojects\myproject\goals\views.py

from django.shortcuts import redirect, render
from django.http import HttpResponse, HttpResponseRedirect

Use HttpResponseRedirect In A Function

We can add a new function in view.py that handles any GET requests that match an integer-based timeframe from the urls.py configuration. Then, we use basic Python to map the integer value to a key in a dictionary that holds the daily, weekly, and monthly goals. Then, we make use of HttpResponseRedirect to redirect the URL of 127.0.0.1:8000/goals/1 to http://127.0.0.1:8000/goals/daily, 127.0.0.1:8000/goals/2 to http://127.0.0.1:8000/goals/weekly, and 127.0.0.1:8000/goals/3 to http://127.0.0.1:8000/goals/monthly.

from django.shortcuts import redirect, render
from django.http import HttpResponse, HttpResponseRedirect

# dictionary data store
goals = {
    'daily': 'Learn Something New',
    'weekly': 'Take a day off',
    'monthly': 'Complete a creative course'
}


def goals_by_int_timeframe(request, timeframe):
    timeframes = list(goals.keys())
    redirect_to = timeframes[timeframe - 1]
    return HttpResponseRedirect('/goals/' + redirect_to)


def goals_by_timeframe(request, timeframe):
    goal = goals[timeframe]
    return HttpResponse(goal)

Visiting the address of 127.0.0.1:8000/goals/1 now produces an immediate 302 redirect to http://127.0.0.1:8000/goals/daily which gives a 200 OK response.

django httpresponseredirect
django 302 status code

How To Use HttpResponseRedirect In Django Summary

We now have a good grasp on requests, responses, and redirects in Django. This little application makes use of path converters to handle both int and strings in a URL pattern, then uses logic to handle each request based on whether the captured value is an int or str. We used Django’s HttpResponseRedirect function to forward integer-based requests to their associated string-based URLs.