Industrial Training




Render HTML Forms (GET & POST) in Django


Django is often called “Batteries Included Framework” because it has a default setting for everything and has features that can help anyone develop a website rapidly. Talking about forms, In HTML, a form is a collection of elements inside < form>…< /form> that allow a visitor to do things like enter text, select options, manipulate objects or controls, and so on, and then send that information back to the server. Basically it is collection of data for processing it for any purpose including saving it in database or fetching data from database. Django supports all types of HTML forms and rendering data from them to a view for processing using various logical operations.


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

Django also provides a built-in feature of Django Forms just like Django Models. One can create forms in Django and use them to fetch data from the user in a convenient manner.

To begin with forms, one needs to be familiar about GET and POST requests in forms.


  • GET : GET, by contrast, bundles the submitted data into a string, and uses this to compose a URL. The URL contains the address where the data must be sent, as well as the data keys and values. You can see this in action if you do a search in the Django documentation, which will produce a URL of the form https://docs.djangoproject.com/search/?q=forms&release=1.
  • POST :Any request that could be used to change the state of the system – for example, a request that makes changes in the database – should use POST.

Render HTML Forms in Django Explanation

Illustration of Django Forms using an Example. Consider a project named mcatutorials having an app named mcatutorials.


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 can you input the data from a user and use it in your view. Enter following code in mcatutorials > templates > home.html

< form action = "" method = "get"> 
    < label for="your_name">Your name: < /label> 
    < input id="your_name" type="text" name="your_name"> 
    < input type="submit" value="OK"> 
< /form> 			  

Now to render it in our view we need to modify urls.py of mcatutorials app Enter following code in mcatutorials > urls.py

from django.urls import path 
  
# importing views from views..py 
from .views import mcatutorials_view 
  
urlpatterns = [ 
    path('', home_view ), 
] 

Now lets move to our home_view and start checking how are we going to get the data. Entire data from a HTML form in Django is transferred as a JSON object called request.

Let’s create a view first and then we will try all methods to fetch data from the form.

from django.shortcuts import render 
  
# Create your views here. 
def home_view(request): 
  
    # logic of view will be implemented here 
    return render(request, "home.html") 

As we have everything set up let us run Python manage.py runserver and check if form is there on the home page.

django-forms-python

By default every form ever written in a HTML makes a GET request to the back end of an application, a GET request normally works using queries in the url. Let’s demonstrate it using above form, Fill up form using your name and let’s check what happens.

python-django-forms-

Above URL is appended with name attribute of input tag and the name entered in the form. This is how the GET request works whatever be the number of inputs they would be appended to the URL to send the data to the back end of an application. Let’s check how to finally get this data in our view so that logic could be applied based on 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 form we can see output in terminal as below

python-django-forms-get-request

request.GET returns a query dictionary which one can access like any other python dictionary and finally use its data for applying some logic. Similarly, if method of transmission is POST, you can use request.POST as query dictionary for rendering the data from the form into views.


In home.html,

< form action = "" method = "POST"> 
    {% csrf_token %}  
    < label for="your_name">Your name:  
    < input id="your_name" type="text" name="your_name"> 
    < input type="submit" value="OK"> 
< /form>

Note that whenever we create a form request, Django requires you to add {% csrf_token %} in form for security purposes
Now, in views.py let’s check what request.POST has got.

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 below.

python-django-forms-render-post-request

This way one can use this data for querying into the database or for processing using some logical operation and pass using the context dictionary to template.




Hi I am Pluto.