How to Redirect a User to Another Page from a Form in Django using the Action Attribute



Python


In this article, we show how to redirect a user to another page from a form in Django using the action attribute.

There's a few ways we can redirect a user to another page once s/he has entered information into a form. You can either use the HttpResponseRedirect() function in the views.py file or you can specify the url that you want to redirect to in the action attribute of the form.

In this article, we show how to redirect a user to another page from a form in Django. And, as stated before, this is done by specifying the URL that you would like to redirect to in the action attribute of the form tag.

This URL can be either hardcoded in or placed in with a dynamic URL.

In the following code below, we will show how to do this with a dynamic URL.

So there is a number of code parts we need to go through and we do this below.

urls.py File in the Root Directory

The first thing we have to modify is the urls.py file in the root directory.

So let's we created an Articles app.

To this app, we want to give the namespace of Articles.

This is shown below.



So you can see here in this code, we have our URL mappings in the root directory.

So we type in, http://127.0.0.1/Articles (or localhost:8000/Articles), then Django then goes to the urls.py file in the Articles app and then looks at those URL mappings. Notice that inside of this include() function, we have a namespace that is set equal to Articles. This is important when creating a dynamic URL, because we need the namespace, along with the URL name, which you will find in the urls.py page in the Articles app.

Next we go to the urls.py file in the Articles app.

urls.py File in the Articles app

Next we need to go to the urls.py file in the Articles app.

In this app, we specify the url name for the success page that you want to redirect the user to when s/he fills in information and submits the form.

This is shown in the code below.



You can see that for the success page, we have a url name of 'successpage'. This is shown by the statement, name='successpage'

This is important because in order to make dynamic urls, we need to know the namespace and the url name for the URL.

home.html Template File

Next we show the original HTML page that contains the form that a user should fill out.



So in this file, we have a header of Articles page and then a form that contains one text box and a submit button. This text box asks for the user's name.

Notice that the method is POST and the action attribute of the form is set equal to, {% url 'Articles:successpage' %}

So in order to create a dynamic URL, you have to specify {% url %}. After url, you specify in single quotes (or double), the namespace, followed by a colon, followed by the url name, and then the closing quote.

When the user submits the form, s/he will then be redirected to this URL page, the success page.

Let's now go to the views.py file.

views.py File

We will now examine the views.py file to see what is going on in there.

In the views.py file, we have the function-based views which defines the view that gets returned for each respective URL.



So the first thing we must do is import render, so that we can render a template file.

We then define the view for the articlespage.

We then define the view for the successpage.

We extract the data that the user enters from the form and save it in the variable, data. Since the text box is an HTML form (not a django form) and its name is textbox1, we get the data and save it in the variable, data, by the line, data= request.POST.get('textbox1')

We then create a context dictionary and pass in the data to the template file, successpage.html

successpage.html Template File

Lastly, we show the successpage.html template file.

This prints out that the information the user has entered has been successfully received and it prints out the name that the user entered.

This is shown below.



So this just prints out that the user has successfully entered in the information and the name entered in.

This is the page that the user will automatically be redirected to once s/he fills out the form and clicks the submit button.

So this is how to redirect a user to another page from a form in Django using the action attribute.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...