Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Django
  • Views
  • Model
  • Template
  • Forms
  • Jinja
  • Python SQLite
  • Flask
  • Json
  • Postman
  • Interview Ques
  • MongoDB
  • Python MongoDB
  • Python Database
  • ReactJS
  • Vue.js
Open In App
Next Article:
Render Django Form Fields Manually
Next article icon

Initial form data - Django Forms

Last Updated : 23 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.

  • How to Create a Basic Project using MVT in Django?
  • How to Create 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.

Read Next: form field custom widgets


Next Article
Render Django Form Fields Manually

N

NaveenArora
Improve
Article Tags :
  • Python
  • Python Django
  • Django-forms
Practice Tags :
  • python

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences