Django Page Redirection - Django

What is Django Page Redirection?

Page redirection is needed for plenty motives in net software. You would possibly need to redirect a person to another web page when a selected movement takes place, or essentially in case of mistakes. As an instance, whilst a consumer logs in to your internet site, he is frequently redirected both to the main home web page and to his non-public dashboard. In Django, redirection is performed the usage of the 'redirect' approach.

The 'redirect' method takes as argument: The URL you want to be redirected to as string A view's call.
The myapp/views seem like the following up to now –

Allow change the howdy view to redirect to and our viewArticle to redirect to our inner '/myapp/articles'. To do so the myapp/ will trade to –

Within the above instance, first we imported redirect from django.shortcuts and for redirection to the Django official internet site we just pass the total URL to the 'redirect' approach as string, and for the second instance (the viewArticle view) the 'redirect' method takes the view call and his parameters as arguments.

Having access to /myapp/hello will provide you with the following screen −

page redirctin-1
And accessing /myapp/article/42, will give you the following screen −

page rediret-2

It is also feasible to specify whether the 'redirect' is temporary or everlasting by including everlasting = authentic parameter. The user will see no difference, but these are details that search engines like google and yahoo recollect while ranking of your website.

Also remember the fact that 'name' parameter we defined in our even as mapping the URLs –

That name (here article) can be used as argument for the 'redirect' method, then our viewArticle redirection can be changed from –

To –

Note − there's additionally a feature to generate URLs; it is used inside the equal manner as redirect; the 'opposite' technique (django.middle.urlresolvers.opposite). This feature does not return a HttpResponseRedirect item, but surely a string containing the URL to the view compiled with any surpassed argument.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

Django Topics