Initial form data – Django Forms

After creating a Django Form, if one requires some or all fields of the form be filled with some initial data, one can use functionality of Django forms to do so. It is not the same as a placeholder, but this data will be passed into the view when submitted. There are multiple methods to do this, most common being to pass the data dictionary when we initialize the form in Django view. Other methods include passing initial values through form fields or overriding the __init__ method.

How to pass initial data to a Django form ?

Illustration of passing initial data using an Example. Consider a project named geeksforgeeks having an app named geeks.

Refer to the following articles to check how to create a project and an app in Django.

Now let’s create a demo form in “geeks/forms.py”,

filter_none

edit
close

play_arrow

link
brightness_4
code

from django import forms
  
// creating a django form
class GeeksForm(forms.Form):
    title = forms.CharField()
    description = forms.CharField()
    available = forms.BooleanField()
    email = forms.EmailField()

chevron_right


Now to render this form we need to create the view and template which will be used to display the form to user. In geeks/views.py, create a view



filter_none

edit
close

play_arrow

link
brightness_4
code

from django.shortcuts import render
from .forms import GeeksForm
  
# creating a home view
def home_view(request):
    context = {}
    form = GeeksForm(request.POST or None)
    context['form'] = form
    return render(request, "home.html", context)

chevron_right


and in templates/home.html,

filter_none

edit
close

play_arrow

link
brightness_4
code

<form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit">
</form>

chevron_right


Now let’s display the form by running

Python manage.py runserver

visit http://127.0.0.1:8000/
initial-form-data-django-forms

Method 1 – Adding initial form data in views.py

This first and most commonly used method to add initial data through a dictionary is in view.py during the initialization of a form. Here is the code of views.py with some added data.

filter_none

edit
close

play_arrow

link
brightness_4
code

from django.shortcuts import render
from .forms import GeeksForm
  
def home_view(request):
    context ={}
  
    # dictionary for initial data with 
    # field names as keys
    initial_dict = {
        "title" : "My New Title",
        "description" : " A New Description",
        "available":True,
        "email":"abc@gmail.com"
    }
  
    # add the dictionary during initialization
    form = GeeksForm(request.POST or None, initial = initial_dict)
  
    context['form']= form
    return render(request, "home.html", context)

chevron_right


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

Method 2 – Adding initial form data using fields in forms.py

One can add initial data using fields in forms.py. An attribute initial is there for this purpose.
In forms.py,

filter_none

edit
close

play_arrow

link
brightness_4
code

from django import forms
  
class GeeksForm(forms.Form):
    # adding initial data using initial attribute
    title = forms.CharField(initial = "Method 2 ")
    description = forms.CharField(initial = "Method 2 description")
    available = forms.BooleanField(initial = True)
    email = forms.EmailField(initial = "abc@gmail.com")

chevron_right


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

django-forms-initial-data-method-2

This way one can add initial data to a form in order to ease the work by a user or any related purpose. This data will be passed to models or views as defined by user and would act as normal data entered by user in the form.




My Personal Notes arrow_drop_up

Software Developer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.