THE FORM IN DJANGO
In this post we will see how to create a contact form in Django. Let’s first see what forms are in Django with slides. I also leave the official link to the documentation Forms.
PREPARATION OF THE ENVIRONMENT
Let’s go over all the fundamental steps for creating a new web application in Django.
- Virtual environment creation that I called venv with the following command: python -m venv venv
- Activate the virtual environment with the command: venv\Scripts\activate by positioning yourself in the root folder chosen for the project.
- Installing Django with the pip install django command
- Upgrade pip if it is not updated with the command: django.exe -m pip install –upgrade pip
CREATION OF THE PROJECT
- Creating a new project that I called DjangoThirdLevel with the following command: django-admin startproject DjangoThirdLevel
- Create a new formsApp application with the following command: python manage.py startapp formsApp by going to the directory where the manage.py file is located.
Once the application has been created, open the project’s settings.py file, register our application, change the language to ‘it’ and set the template for the two HTML files that we will need.
As set in the TEMPLATES we create two folders, one that we call templates under the formsApp directory and we nest a second folder that we will call as the formsApp application. Within the latter we create two HTML files, home.html and contatto.html. Let’s go to the Bootstrap site, copy the starter template and paste it into the two pages.
Let’s go to the application’s views.py file and manage these two HTML templates.
Let’s record the views in the file urls.py.
We start our development server and verify that everything is working. Now that we have everything set up, let’s create the forms.py file within the application. We can now go and define in this file all the various fields of object-oriented programming that we need, in addition to the fields we can define the labels, the widgets we want to use, specify the length of the field with max_length etc. Let’s see how I created the FormContatto class.
Now let’s change the view so that its context passes the newly created form to the HTML page. Once this is done, let’s try to fill out the contact form. You will see that you get an error.
This is because the CSRF verification failed. This is a security measure implemented in Django that checks for Site Request Forgery attacks. We can use this tag after the form tag to have a secure transaction.
{% csrf_token %}
Once the error has been fixed, if we check the output we realize that the request was successful (Status Code = 200) however our view is not yet prepared to process the data sent. Let’s see how to change it.
As you can see from the code above, the first thing we are going to check is if we are in GET or in POST. If we are in POST we can create the FormContatto form passing all the request data to it. Conversely, if it’s not a POST request, else branch, I’m just going to initialize the FormContatto form. If we are in POST and the form is valid I go to show the data entered in the console and the request returns with an HttpResponse. Conversely, if the form is invalid, the data is presented to the user again for correction.
Leave A Comment