Initial form data - Django Forms
Last Updated :
12 Jul, 2025
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 multiple ways to set initial data for forms. The most common is passing a dictionary of initial values when initializing the form in your view. Other options include setting initial values directly on form fields or overriding the form’s __init__ method for more dynamic behavior.
Let’s explore how to set initial data in a Django form using a simple example. Assume we have a project called geeksforgeeks and an app called geeks.
Refer to the following articles to check how to create a project and an app in Django.
In geeks/forms.py, define a basic Django form:
Python
from django import forms
class GeeksForm(forms.Form):
title = forms.CharField()
description = forms.CharField()
available = forms.BooleanField()
email = forms.EmailField()
In geeks/views.py, create a view to display the form:
Python
from django.shortcuts import render
from .forms import GeeksForm
def home_view(request):
form = GeeksForm(request.POST or None)
return render(request, "home.html", {'form': form})
In templates/home.html, add the form rendering code:
HTML
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
Run the Django development server:
python manage.py runserver
Blank FormMethods to Add Initial Data
Method 1: Pass Initial Data in the View (Most Common)
You can pass a dictionary with initial field values when instantiating the form in your view:
Python
from django.shortcuts import render
from .forms import GeeksForm
def home_view(request):
initial_data = {
"title": "My New Title",
"description": "A New Description",
"available": True,
"email": "[email protected]"
}
form = GeeksForm(request.POST or None, initial=initial_data)
return render(request, "home.html", {'form': form})
Now open http://127.0.0.1:8000/. This method is senior of all and will override any data provided during other methods.
Form with DataExplanation:
- When you visit the form page, the fields are pre-filled with the values specified in initial_data.
- This initial data acts as actual form data and will be submitted if the user does not change it.
- This method takes precedence over any other initial values specified elsewhere.
Alternatively, you can specify initial values directly when defining fields in your form class:
Python
from django import forms
class GeeksForm(forms.Form):
title = forms.CharField(initial="Method 2 Title")
description = forms.CharField(initial="Method 2 Description")
available = forms.BooleanField(initial=True)
email = forms.EmailField(initial="[email protected]")
Now visit, http://127.0.0.1:8000/. One can see the data being updated to method 2.
Form with DataExplanation:
- When the form renders, fields show these default values.
- If you also pass initial data in the view (Method 1), those values will override these.
- Useful when you want a static initial value that applies everywhere the form is used.
Read Next: form field custom widgets
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice