Render HTML Forms (GET & POST) in Django
Last Updated :
17 May, 2025
Django is often called a "Batteries Included Framework" because it provides built-in settings and features that help developers build websites rapidly and efficiently. One of the essential components in web development is handling HTML forms, a way for users to send data to the server for processing, such as saving it to a database or retrieving information.
An HTML form is a collection of elements wrapped inside <form>...</form> tags. These elements allow visitors to input text, select options, manipulate controls, and submit data to the server. The server then processes this data, often involving database operations or other logic. Django fully supports all types of HTML forms and simplifies the rendering and processing of form data using views.
To know more about HTML forms, visit HTML | form Tag.
Django provides a powerful feature called Django Forms that abstracts HTML forms and their handling. It’s similar in concept to Django Models, letting you define forms as Python classes. However, before diving into Django Forms, it’s important to understand the basics of GET and POST methods in standard HTML forms.
Understanding GET and POST Methods
1. GET: This method appends form data to the URL as query parameters. For example, when you search in the Django documentation, the URL might look like:
https://docs.djangoproject.com/search/?q=forms&release=1
2. POST: This method sends the form data in the body of the HTTP request. POST is used when the request modifies data on the server, like updating a database.
Let’s illustrate rendering forms in Django with a simple example. Suppose you have a Django project named geeksforgeeks with an app called geeks.
Create a template file home.html inside geeks/templates/ and add the following:
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>
This form will send data using the GET method when submitted.
Step 2: Setup URL Configuration
In your app’s urls.py (geeks/urls.py), set up the URL pattern:
Python
from django.urls import path
from .views import home_view
urlpatterns = [
path('', home_view),
]
Step 3: Create the View
In views.py (geeks/views.py), create the view to render the form:
Python
from django.shortcuts import render
def home_view(request):
return render(request, "home.html")
Step 4: Run the Server and Test
Run your Django server:
python manage.py runserver
Handling GET Request Data in Views
When you submit the form, the data will be appended to the URL. To access this data in the view, you can use request.GET:
Python
from django.shortcuts import render
def home_view(request):
print(request.GET) # Prints the submitted data as a QueryDict
return render(request, "home.html")
If you enter a name and submit the form, you will see the data printed in your terminal, like:
GET Requestrequest.GET returns a query dictionary that one can access like any other python dictionary and finally use its data for applying some logic.
Handling POST Requests in Django Forms
To use POST instead of GET, modify your form in home.html:
HTML
<form action="" method="POST">
{% csrf_token %}
<label for="your_name">Your name: </label>
<input id="your_name" type="text" name="your_name">
<input type="submit" value="OK">
</form>
Note: Django requires the {% csrf_token %} template tag inside all POST forms for security to prevent Cross-Site Request Forgery attacks.
Update the View to Handle POST Data
Modify your view to handle POST data:
Python
from django.shortcuts import render
def home_view(request):
if request.method == "POST":
print(request.POST) # Prints the POSTed data as a QueryDict
name = request.POST.get('your_name')
return render(request, "home.html")
Now when we submit the form it shows the data as below:
POST RequestThis 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 the template.
Similar Reads
Django Form Django Forms are used to gather input from users, validate that input, and process it, often saving the data to the database. For example, when registering a user, a form collects information like name, email, and password.Django automatically maps form fields to corresponding HTML input elements. I
5 min read
How to create a form using Django Forms ? This article explains how to create a basic form using various form fields and attributes. Creating a form in Django is very similar to creating a model, you define the fields you want and specify their types. For example, a registration form might need fields like First Name (CharField), Roll Numbe
2 min read
Django ModelFormSets Django ModelFormsets provide a powerful way to manage multiple model-based forms on a single page. They allow you to create, update, or delete multiple instances of a model at once, using a group of forms that correspond to the model's fields.Think of ModelFormsets as a collection of forms linked to
2 min read
Django ModelForm ModelForm is a class that automatically generates a "form" from a Django model. It reduces boilerplate code by linking your form fields directly to your model fields, making form creation faster, cleaner, and less error-prone. It also provides built-in methods and validation to streamline form proce
3 min read
Render Django Forms as table Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent
2 min read
Render Django Forms as paragraph Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent
2 min read
Render Django Forms as list Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent
2 min read
Render HTML Forms (GET & POST) Django is often called a "Batteries Included Framework" because it provides built-in settings and features that help developers build websites rapidly and efficiently. One of the essential components in web development is handling HTML forms, a way for users to send data to the server for processing
3 min read
Django form field custom widgets A widget is Djangoâs representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. Whenever you specify a field on a form, Django will use a default widget that is appropriate to the type o
3 min read
Initial form data _ Django Forms When using Django forms, we may want to automatically fill in some fields with default values. This is called initial data and it's different from placeholders because it actually fills the fields. When the form is submitted, this data is treated just like anything the user types in.Django offers mu
3 min read
Render Django Form Fields Manually Django form fields have several built-in methods to ease the work of the developer but sometimes one needs to implement things manually for customizing User Interface(UI). We have already covered on How to create and use a form in Django?. A form comes with 3 in-built methods that can be used to ren
5 min read
Django Formsets Django Formsets allow you to manage multiple instances of the same form on a single webpage easily. Instead of creating separate forms one by one, formsets let you group them together so you can display, validate and process all of them at once. Think of a formset like a spreadsheet where each row i
3 min read
Django Forms Django Forms are used to gather input from users, validate that input, and process it, often saving the data to the database. For example, when registering a user, a form collects information like name, email, and password.Django automatically maps form fields to corresponding HTML input elements. I
5 min read