Menu Close

How to set default form values with Django Python?

To set default form values with Django Python, we create the form instance with the default values in the constructor arguments.

Or we can put the default values in the form definition.

For instance, we write

form = JournalForm(initial={'tank': 123})

to create a JournalForm instance with the initial argument set to a dictionary with the initial form values.

In the form definition, we can add a field with its default value by writing

tank = forms.IntegerField(widget=forms.HiddenInput(), initial=123) 

to set the initial value using the field constructor with the initial argument set.

Posted in Python, Python Answers