There are two ways to fill out a Django form.
The first is to pass the dictionary as the first argument when creating it (or pass it as data kwarg, which is one and the same). This is what you do when you want to use the POST data to fill out and validate the form.
data_dict = {'charfield1': 'data1', 'charfield2': 'data2', 'choicefield': 3} form = MyForm(data_dict)
However, this will lead to a validation of the form, so it only works if you really provide valid and complete data to begin with, otherwise you will start with errors.
Another way to fill out a form is to use the initial parameter (documented here ). This gives initial values ββfor the form fields, but does not cause validation. Therefore, it is suitable if you do not fill in all the values, for example.
form = MyForm(initial=data_dict)
To populate the selection box with initial , use the pk value.
Daniel Roseman Jun 01 '09 at 20:31 2009-06-01 20:31
source share