Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Django is a high-level Python framework. It allows you to develop fast and robust websites even driven by complex databases. It enables hassle-free website development, which has made it famous. Big tech giants like Instagram, Spotify, YouTube, etc, use it. And, when we talk about a website, it can contain more than one webpage. Thus, switching between those pages on specific points is essential when programming a website.
In this article, we will study Django Redirects.
What is Django Redirect?
Django Redirect redirects the user from one webpage to another upon some event. This event can be precisely defined, or it can be an error. The most common example is when a user logs into some website, they are redirected to the dashboard or the home page.
In Django, the parameter passed to the redirect function is the webpage URL to which it will redirect the user. Let us understand this with a simple example of the submission of a form.
Then, create a folder for your Django project, open its path in your terminal, and write the following code to create a folder for your Django project with your desired name.
django-admin startproject <project_name>
You can also try this code with Online Python Compiler
Then, you will see a folder with your entered name made in the folder with some .py files for the Django project. You can use the following command in your folder’s location in the terminal to locally run the project.
python manage.py runserver 4444
You can also try this code with Online Python Compiler
We have imported HttpResponseRedirect for using the redirecting option.
The fromm function redirects the user to the URL of our homepage, which we have seen in our urls.py file. It uses the req.POST.get() function to get the two values we have taken from the user when he/she has filled out the form. Then, we store it in data as two different values and one more for their concatenation. Then, we redirect the user to the homepage of the website.
So, after running the server and going to the URL as follows, we should fill out a form and submit it to be redirected to the home page.
Next, after we submit it, it redirects us to the home page.
It is the process of redirection that we have achieved. We can also edit some of our codes to get the concatenated value of our responses on the homepage.
The homepage in the view.py file is edited as follows:
Python
Python
def homepage(req): if req.method=="GET": output=req.GET.get('output') return render(req, "index.html", {'output':output})
You can also try this code with Online Python Compiler
Here, the {{output}} denotes the passed value to it, which we coded in our views.py file in the homepage function.
When we enter the values in our form, they are also passed to the redirected page.
OUTPUT
Thus, we can achieve different results using Django Redirects to our requirements. Now, there are two types of Django redirect: temporary and permanent. The permanent one makes permanent changes to the URL. Both of these are for search engine optimization.
Frequently Asked Questions
What is Django?
Django is a high-level Python framework. It allows you to develop fast and robust websites even driven by complex databases. It enables hassle-free website development, which has made it famous. Big tech giants like Instagram, Spotify, YouTube, etc, use it.
What do you mean by Django Redirect?
Django Redirect redirects the user from one webpage to another upon some event. This event can be precisely defined, or it can be an error. The most common example is submitting a form and getting redirected to a different page.
How do redirections between web pages help the user?
It helps the user navigate smoothly through different web pages on the website. Also, performing operations on the application may lead to some inconveniences, and sometimes redirects prevent accidental operations from happening twice.
Conclusion
Django is a high-level and famous Python framework for building web applications. In this article, we specifically studied Django Redirect. We looked into its definition, Django’s installation, and its uses. Then, we learned it with the help of an example and saw the working with different cases.
If you are interested in learning more about this topic, read the following articles:-