48👍
Here’s the key part from the django docs on bound and unbound forms.
A Form instance is either bound to a set of data, or unbound:
- If it’s bound to a set of data, it’s capable of validating that data and rendering the form as HTML with the data displayed in the HTML.
- If it’s unbound, it cannot do validation (because there’s no data to validate!), but it can still render the blank form as HTML.
You can’t really see the difference for the example form you gave, because the form is valid in the "bound data" style. Let’s extend the form by adding an age
field, then the difference will be more obvious.
class MyForm(forms.Form):
name = forms.CharField()
age = forms.IntegerField()
Bound form
my_form = MyForm({'name': request.user.first_name})
This form is invalid, because age
is not specified. When you render the form in the template, you will see validation errors for the age
field.
Unbound form with dynamic initial data
my_form = MyForm(initial={'name':request.user.first_name})
This form is unbound. Validation is not triggered, so there will not be any errors displayed when you render the template.
8👍
No, that’s not what the difference is (and I’d be interested to know where in the documentation you got that impression from). The difference is whether validation is performed.
Initial data does not trigger validation. This allows you, for example, to pre-fill certain fields but leave others empty, even though they are required. If you used bound data, you would get errors for those empty required fields even on the first viewing of that form, which would be annoying for the user.
Bound data, of course, triggers validation. Also, if you’re using a modelform, the related instance will only be updated with bound data, not initial data.
- [Django]-How to add annotate data in django-rest-framework queryset responses?
- [Django]-Django urls without a trailing slash do not redirect
- [Django]-How to redirect on conditions with class based views in Django 1.3?
4👍
Another difference is that data
expects something that widgets can parse whereas initial
is per-field. This makes a difference if you e.g. use MultiWidget
s. In such case data
should contain something like
{'myfield_0': 'data for subwidget 0',
'myfield_1': 'data for subwidget 1'}
whereas initial
expects something like this:
{'myfield': 'data for subwidget 0,data for subwidget 1'}
- [Django]-Count() vs len() on a Django QuerySet
- [Django]-Get multiple rows with one query in django?
- [Django]-Python Requests POST doing a GET?