How to Redirect to Another Page in Django 


               


How to Redirect to Another Page in Django



Python


In this article, we go over how to redirect to another page in Django.

So, if a user types in a certain URL into a web browser or clicks a submit button of a form and you want the person to be redirected to another page, this can be easily done in Django.

In this article, we'll simply show how to redirect a user to another page when typing in a certain URL into the web browser.

Basically, we'll just be working with the views.py page and the urls.py page.

So, let's get into how to do this.

views.py File

So, let's start with the views.py file.

So, in this views.py file, we create a function that redirects the user to whatever page we want.

This page can be on our website or a completely separate website.

It doesn't matter. Django has the capability of doing either.

In this example, we'll redirect a user to the dropbox website.

The code creates a function called dropboxgo that redirects a user to the dropbox website.



So, the above code creates a function called dropboxgo() that redirects a user to the dropbox website.

This is done through the HttpResponseRedirect object.

We must first import this object from django.http

Now, we just have to go to the urls.py file, so that we can redirect a user to the dropbox website when s/he types in a certain URL.


urls.py File

So the code contained in the urls.py file is shown below.



So, we import url from django.conf.urls

We then have to import the function, dropboxgo, that we created in the views.py file.

We then specify that if a user types in dropbox into the url, the function dropboxgo will be executed, which is what redirects a user to the dropbox website.

If the path specified in the urls.py file in the project root directory is, url(r'^', include('homepage.urls')), then if the user types in http://127.0.0.1:8000/dropbox, the user will be redirected to the dropbox website.

So that is just quickly how to redirect to another web page with Django.


How to Redirect to Another Page on the Same Website

So, we've now shown how to redirect to another page on a different website.

We will now give an example of how to redirect to another page on the same website.

So, whatever you redirect to has to be specified in one of the urls in one of the urls.py files in one of the apps.

So, let's say, a user types into the web browser, http://127.0.0.1:8000/Blog, we want to redirect the user to http://127.0.0.1:8000/Blog/today/now

So how do we do this?

So, let's go the views.py file first.

Again, we create a function that redirects a user to the page that we want, which in thsi case is http://127.0.0.1:8000/Blog/today/now

So, the views.py file would have the following code.



Remember when dealing with the HttpResponseRedirect object in Django, the path that you specify is relative to the url path specified in the urls.py file in the root directory.

Thus, if you using the HttpResponseRedirect object in the Blog app, the path you choose is determined by the url path in the urls.py file in the project root directory.

Thus, if what is specified in the urls.py file is, url(r'^Blog/', include('Blog.urls')), the url path begins at, http://127.0.0.1:8000/Blog/

Thus, if we specify in the views.py file, return HttpResponseRedirect("today/now"), the full path of redirection will be to, http://127.0.0.1:8000/Blog/today/now

So, now that that's in place, we just have to modify the urls.py page.

So the urls.py page would contain the following code.



Redirecting to another page on your website can be a little tricky, as mentioned before.

This is because you have to take into account the urls path in the urls.py file in the root directory.

If the urls path in the root directory contains, for instance, url(r'^Blog/', include('Blog.urls')), then the HttpResponseRedirect in the views.py file in that App already begins with, http://127.0.0.1:8000/Blog/

Thus, if you specify, return HttpResponseRedirect("today"), the redirect will then be to, http://127.0.0.1:8000/Blog/today/now

If you specify, return HttpResponseRedirect("Blog/today"), the redirect will then be to, http://127.0.0.1:8000/Blog/Blog/today/now

So when redirecting to pages on your website, you have to know the url path specified in the urls.py file in the root directory. That path will be where you go relative from when using the HttpResponseRedirect. It's not the root directory but in whatever app you're using, the url path in the root directory for that app.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...