English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Django Page Redirection

Page redirection is necessary for many reasons in web applications. You may want to redirect users to another page when a specific action occurs or in case of an error. For example, when a user logs into a website, they are often redirected to their homepage or personal dashboard. In Django, redirection is achieved using the 'redirect' method.

In the 'redirect' method, the view name needs to be as a parameter: the string of the URL to be redirected to.

myapp/views so far are as follows −

# Filename: example.py
# Copyright : 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date : 2020-08-08
def hello(request):
    today = datetime.datetime.now().date()
    daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    return render(request, "hello.html", {"today": today, "days_of_week": daysOfWeek})
 
 def viewArticle(request, articleId):
    """A view that displays an article based on its ID"""
    text = "Displaying article Number: %s" % articleId
    return HttpResponse(text)
 
 def viewArticles(request, year, month):
    text = "Displaying articles of: %s"/%s"%(year, month)
    return HttpResponse(text)

Let's modify 'hello' to redirect to 'djangoproject.com', and 'viewArticle' to redirect to our internal '/myapp/articles' in 'myapp'/The 'view.py' will be modified as follows: 

# Filename: example.py
# Copyright : 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date : 2020-08-08
from django.shortcuts import render, redirect
 from django.http import HttpResponse
 import datetime
 # Create your views here.
 def hello(request):
    today = datetime.datetime.now().date()
    daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    return redirect("https://www.djangoproject.com)
 
 def viewArticle(request, articleId):
    """A view that displays an article based on its ID"""
    text = "Displaying article Number: %s" % articleId
    return redirect(viewArticles, year = "2045", month = "02")
 
 def viewArticles(request, year, month):
    text = "Displaying articles of: %s"/%s"%(year, month)
    return HttpResponse(text)

In the above example, we first import the redirect (redirect) from Django. Shortcut and redirect to the Django official website, we just need to use the complete URL to the 'redirect' method as a string, in the second instance (in the 'viewArticle' view), we take the view name and its parameters as arguments for the 'redirect' method.

Access/myapp/hello, and the following screen will be displayed-

and access /myapp/article/42, which will display the following screen-

You can also specify whether 'redirect' is temporary or permanent by adding the 'permanent = True' parameter. There will be no difference for users to see, but these are details that search engines consider when ranking websites.

We define the 'name' parameter in the 'url.py' when mapping URLs.

# Filename: example.py
# Copyright : 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date : 2020-08-08
url(r'^articles'/(?P\d{2)/(?P\d{4)/', 'viewArticles', name = 'articles'),

The name (the article here) can be used as an argument for the 'redirect' method, then the redirection of viewArticle can be modified -

# Filename: example.py
# Copyright : 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date : 2020-08-08
def viewArticle(request, articleId):
    """A view that displays an article based on its ID"""
    text = "Displaying article Number: %s" % articleId
    return redirect(viewArticles, year = "2045", month = "02")

Modify to -

# Filename: example.py
# Copyright : 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date : 2020-08-08
def viewArticle(request, articleId):
    """A view that displays an article based on its ID"""
    text = "Displaying article Number: %s" % articleId
    return redirect(articles, year = "2045", month = "02")

Note - There is a function to generate URL; it is used in the same way for redirection; the 'reverse' method (django.core.urlresolvers.reverse). This function does not return an HttpResponseRedirect object, but simply contains the URL and any compiled view string with any parameters passed in.