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 djangoproject.com and our viewArticle to redirect to our inner '/myapp/articles'. To do so the myapp/view.py 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 −
And accessing /myapp/article/42, will give you the following screen −
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 url.py 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 –
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.
Django Related Tutorials
|Python Tutorial||Java Script Tutorial|
|PHP Tutorial||Ruby on Rails Tutorial|
|XHTML Tutorial||Apache Solr Tutorial|
Django Related Interview Questions
|Python Interview Questions||Java Script Interview Questions|
|Ruby on Rails Interview Questions||Ruby Interview Questions|
|MongoDB Interview Questions|
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.