How to Create a Confirmation Page from an HTML form with Python in Django



Python


In this article, we show how to create a confirmation page from an HTML web form with Python in Django.

So, say you have a form asking a user for his/her first name, last name, and emai.

And after they fill in the form and press the 'Submit' button, you want to bring them to a confirmation page, saying something such as, Thank you. You have successfully completed the form." Something like that.

We'll show how to do that in this page.

So, first thing, let's create the web form we're going to use.

In this page, we are going to use the Django form class, not code the form in plain HTML.

So, normally when you use a Django form, you would create a separate file to house the form, usually called forms.py.

So, we will create this forms.py file now.


forms.py File

So, the code in our forms.py file is shown below.



So we have a standard form, asking a user for his/her first name, last name, and email.


Template file- index.html

We then have our template file, which we call index.html.

In this file, we merge this form.py form into the HTML <form></form> tags to complete the entire form.

Below is the code of the template file.



So, basically, we have html and head tags, as any standard HTML page would have.

In the body tag, we have our form.

We set the form action attribute to "". We will retrieve the data from the views.py file.

The method "POST" because we want the data in our script, not in the URL like as what happens with the GET method.

As with all forms, to prevent cross site request forgery, we insert the line, {% csrf_token %}

With the line, {{ form.as_p }}, we import our form from the forms.py file. as_p means we lay it our with each element of the form wrapped around in

(HTML paragraph) tags.

We then create a submit button and close the form.

We will now go on to the views.py file.


views.py File

So in our views.py File is where we redirect a user to the confirmation page.



So, in this code, we import render, since, we render in the template which has the form.

We import the form we created, UserForm, in the forms.py file.

We then import from django.http HttpResponseRedirect. This function is redirects a user to another page.

We then have our index function with the parameter, request.

We create a variable named form and set it equal to UserForm()

We then create a variable called formresult and set it equal to an empty string.

We then find out if the request method is POST. If it is we set the variable, formresult, equal to UserForm(request.POST).

If the data that the user entered is valid, then we redirect the user to the Confirmationpage App.

The first time Django goes through the views.py file, the if statement, formresult.is_valid(), will be false, because the user hasn't entered in any data that is valid.

The second time Django goes through the file after the user has entered valid data, the if statement will be true, and the user will be redirected to the confirmation page app.

So, now all we have to do is create a quick confirmation page.


Confirmation page

So, I just created an app called Confirmationpage. I set up all of the urls.py files, setting.py file, etc.

I created the following very simple view.py file in the Confirmationpage app.

The following is the code shown below.



We simply print out, Thank you. We have received your form

This is the page a user will be redirected to after filling out the form.

Django confirmation page from an HTML form

So this is producing confirmation pages with Python in Django in a nutshell.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...