Open In App

Initial form data - Django Forms

Last Updated : 15 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

How to Pass Initial Data to a Django Form?

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.

Step 1: Create a Demo Form

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()

Step 2: Create a View to Render the Form

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})

Step 3: Create a Template to Display the 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>

Step 4: Run the Server and Visit the Form

Run the Django development server:

python manage.py runserver

initial-form-data-django-forms
Blank Form

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

initial-data-django-forms
Form with Data

Explanation:

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

Method 2: Set Initial Values on Form Fields in forms.py

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.

django-forms-initial-data-method-2
Form with Data

Explanation:

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

Next Article
Practice Tags :

Similar Reads