+

Rendering HTML Forms (GET & amp; POST) in Django

Django is often referred to as the "Battery Included Framework" because it has a default setting for everything and has features that can help anyone develop a website quickly. Speaking of forms, in HTML the form — it is a set of elements inside & lt; form & gt; ... & lt; / form & gt; that allow the visitor to do things like enter text, select options, manipulate objects or controls, etc., and then send the information back to the server. In essence, this is the collection of data for their processing for any purpose, including storing them in a database or fetching data from a database. Django supports all types of HTML forms and outputs data from them to the view for processing using various boolean operations.

To know more about HTML forms, visit HTML | form Tag .

Django also provides a built-in function for Django forms, as well as Django models . You can create forms in Django and use them to conveniently retrieve data from the user.

To start with forms, you need to know about GET and POST requests in forms.

  • GET : GET, on the other hand, concatenates the submitted data into a string and uses this is for composing the url. The URL contains the address to which the data should be sent, as well as the data keys and values. This can be seen in action if you search the Django documentation, which will return a URL in the form https://docs.djangoproject.com/search/?q=forms&release=1 .
  • POST : Any request that can be used to system state changes, such as a query that makes changes to the database must use POST.

HTML Form Rendering in Django Explained

Django Form Illustration for example. Consider a project named pythonengineering that has an application named geeks .

Refer to the following articles to check how to create a project and an app in Django.

Let`s create a simple HTML form to show how you can enter data from the user and use it in your view. Enter the following code into geeks & gt; templates & gt; home.html

& lt; form action = " " method = "get" & gt; 

& lt; label for = "your_name" & gt; Your name: & lt; / label & gt; 

& lt; input id = "your_name" type = "text" name = "your_name" & gt; 

& lt; input type = "submit" value = "OK" & gt; 

& lt; / form & gt; 

Now, to display it in our view, we need to change urls.py from geeks app
Enter the following code into geeks & gt; urls.py

from django.urls import path

 
# importing views from views..py

from . views import geeks_view

  

urlpatterns = [

path (``, home_view),

]

Now let`s go to our home_view and start checking how we going to get the data. All data from an HTML form to Django is submitted as a JSON object called a request. 
Let`s create a view first and then try all the methods for fetching data from a form.

from django.shortcuts import render

 
# Create your views here.

def home_view (request):

 

# the presentation logic will be implemented here

return render (request, "home.html" )

Since we have all the settings Okay, let`s run Python manage.py runserver and check if there is a form on the main page. 

By default, every form ever written in HTML sends a GET request to the backend of the application, A GET request usually works using URL requests. Let`s demonstrate this using the form above, fill out the form using your name, and let`s check what`s going on. 

The name attribute of the input tag and the name entered in shape. This is how a GET request works, no matter how many inputs they will add to the url to send data to the back end of the application. Let`s check out how to finally get this data in our view so that the logic can be applied based on the input. 
In views.py ,

from django.shortcuts import render

 
# Create your views here.

def home_view (request):

print (request.GET)

return render (request, "home.html" )

Now when we fill out the form, we can see the output in the terminal as shown below

request.GET returns a query dictionary that can be accessed like any other Python dictionary and finally use its data to apply some logic. 
Similarly, if the transfer method is — POST, you can use request.POST as a request dictionary to render data from form to views.

In home.html ,

& lt; form action = " " method = "POST" & gt; 

{% csrf_token%} 

& lt; label for = "your_name" & gt; Your name: & lt; / label & gt; 

& lt; input id = "your_name" type = "text" name = "your_name" & gt; 

& lt; input type = "submit" value = "OK" & gt; 

& lt; / form & gt; 

Please note that whenever we create a form request , Django requires you to add {% csrf_token%} to the form for security.
Now in views.py , let`s check what the request received. POST .

from django.shortcuts import render

  
# Create your views here.

def home_view (request):

print (request.POST)

return render (request, "home.html" )

Now when we submit the form, it shows the data as shown below. 

Thus, you can use this data to query the database or to process using some logical operation and pass the context dictionary to the template.

Get Solution for free from DataCamp guru