Thursday, 15 July 2021

*Episode 9* DJANGO (Page Redirection of Django)

 


Page Redirection


     This property is very useful in web apps. Sometimes, something might occur and the user may need to be redirected to another page. This is what page redirection is useful for. To achieve redirection in Django, we use the “redirect” method. This method takes the URL of redirection as the parameter.


     Consider the code given below for the file “myapplication/views”:


def hello(request):

day = datetime.datetime.now().date()

dWeek = [‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’,

‘Sunday’]

return render(request, “hello.html”, {“today” : day, “days_of_week” : dWeek})

def vwArticle(request, artId):

””” A view for displaying an article based on the ID”””

text = “Displaying the Number of the article: %s” %artId

return HttpResponse(text)

def vwArticles(request, year, month):

text = “Displaying the articles of : %s/%s”%(year, month)

return HttpResponse(text)


    We now need to redirect the “hello” view to the “myproject.com” and the “vwArticle” to be redirected to the internal “/myapplication/articles”. The code for “myapplication/view.py” will have to be changed to the following:


from django.shortcuts import render, redirect

from django.http import HttpResponse

import datetime

# Creating your own views here.

def hello(request):

day = datetime.datetime.now().date()

dWeek = [‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’,

‘Sunday’]

return redirect(“https://www.myproject.com”)

def vwArticle(request, artId):

””” A view for displaying an article based on the ID”””

text = “Displaying the Number of the article : %s” %artId

return redirect(vwArticles, year = “2050”, month = “02”)

def vwArticles(request, year, month):

text = “Displaying the articles of : %s/%s”%(year, month)

return HttpResponse(text)


     The redirect property can also be specified to be either permanent or temporary. Although the users may see this as being negligible, it is important as the search engines will use it for the purpose of ranking your website.


     The “name” parameter was also defined in the file “url.py” during the process of mapping of the URLs as shown below:


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


     The name can be used as an argument for the purpose of redirection, and the “vwArticle” can be changed from the following:


def vwArticle(request, articleId):

””” A view for displaying an article based on the ID”””

text = “Displaying the Number of the article : %s” %artId

return redirect(vwArticles, year = “2050”, month = “02”)


     To the following:


def vwArticle(request, artId):

””” A view for displaying an article based on the ID”””

text = “Displaying the Number of the article : %s” %artId

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


👈Episode 8(D).                                        Episode 10(D)👉

Share This Post

PRINT THIS POST

No comments:

Post a Comment

If you have any doubts. Please let me know.

Featured post

*Episode 1* MCQ for Govt. Job/ Private Job/ MNCs

  Topic:- One Word Substitution 1) Especially skilled in storytelling  Answer:- Raconteur 2) Fear of loneliness Answer:- Eremophobia  3) Usa...