Django Page Redirection - httpresponseredirectX | W3School

There are several reasons for which you are required to have the facility of page redirection. From an action based page redirection to the one where you are required to resolve an error. For an instance, if a user enters a website, he/she gets redirected to the home page of the website or after the login process, he/she gets redirected to the personal dashboard. Django supports this feature by the use of “redirect” method.

Also Read: Django URL Mapping

The redirect method allows passing arguments to it. This argument needs to be a URL that is to be redirected as string A view’s name.

The content of the myapp/views.py file will appear as the following:

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 display an article based on his 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)

[post_middile_section_ad]

In the first example, we will be redirecting the hello view to the djangoproject.com. And to perform this redirection, we need to pass the complete URL to the redirect method as in the string format.

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 display an article based on his 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)

[post_middile_section_ad]

In the second example, the parameters passed to the redirect method are the name of the view (in this case, viewArticle view) and its corresponding parameters.

Now, whenever you try to access /myapp/hello, you will be redirected to the following screen.

page-direction-example

But, when you type in /myapp/article/42 the screen will appear the same as before.

page-direction-example2You can also add an attribute to check whether the redirection is on a temporary or permanent basis. For this, you are required to use the ‘permanent = true’ parameter. This code will not be visible to the end users but it will help the search engines to appropriately rank your website.

We hope you remember the ‘name’ parameter that we have already defined in the url.py file.

url(r’^articles/(?P\d{2})/(?P\d{4})/’, ‘viewArticles’, name = ‘articles’),

Well, this ‘name’ parameter can also be used as the argument to be passed to the redirect method. But, in such a case, make sure you perform the following changes in your viewArticle view.

The present content of the viewArticle view is:

def viewArticle(request, articleId):

  “”” A view that display an article based on his ID”””

  text = “Displaying article Number : %s” %articleId

  return redirect(viewArticles, year = “2045”, month = “02”)

And you are required to change it to:

def viewArticle(request, articleId):

  “”” A view that display an article based on his ID”””

  text = “Displaying article Number : %s” %articleId

  return redirect(articles, year = “2045”, month = “02”)

[post_middile_section_ad]

Note – There is a function with which you can generate URLs. It works similarly to the redirect method and the reverse method (django.core.urlresolvers.reverse). This function returns a string value rather than an HttpResponseRedirect object. The returned string contains the URL to the view compiled with any passed argument.