A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/initial-form-data-django-forms/ below:

Initial form data - Django Forms

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.

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

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": "abc@gmail.com"
    }
    
    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 Data

Explanation:

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="abc@gmail.com")

Now visit, http://127.0.0.1:8000/. One can see the data being updated to method 2.

Form with Data

Explanation:

Read Next: form field custom widgets



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4