Categories
Python Answers

How to set default form values with Django Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *