174
177
{'url': [u'This field is required.'], 'name': [u'This field is required.']}
179
Instead of a constant, you can also pass any callable::
182
>>> class DateForm(forms.Form):
183
... day = forms.DateField(initial=datetime.date.today)
185
<tr><th>Day:</th><td><input type="text" name="day" value="12/23/2008" /><td></tr>
187
The callable will be evaluated only when the unbound form is displayed, not when it is defined.
245
257
In the `built-in Field classes`_ section below, each ``Field`` defines the
246
258
error message keys it uses.
248
Dynamic initial values
249
----------------------
251
The ``initial`` argument to ``Field`` (explained above) lets you hard-code the
252
initial value for a ``Field`` -- but what if you want to declare the initial
253
value at runtime? For example, you might want to fill in a ``username`` field
254
with the username of the current session.
256
To accomplish this, use the ``initial`` argument to a ``Form``. This argument,
257
if given, should be a dictionary mapping field names to initial values. Only
258
include the fields for which you're specifying an initial value; it's not
259
necessary to include every field in your form. For example::
261
>>> class CommentForm(forms.Form):
262
... name = forms.CharField()
263
... url = forms.URLField()
264
... comment = forms.CharField()
265
>>> f = CommentForm(initial={'name': 'your username'}, auto_id=False)
267
<tr><th>Name:</th><td><input type="text" name="name" value="your username" /></td></tr>
268
<tr><th>Url:</th><td><input type="text" name="url" /></td></tr>
269
<tr><th>Comment:</th><td><input type="text" name="comment" /></td></tr>
270
>>> f = CommentForm(initial={'name': 'another username'}, auto_id=False)
272
<tr><th>Name:</th><td><input type="text" name="name" value="another username" /></td></tr>
273
<tr><th>Url:</th><td><input type="text" name="url" /></td></tr>
274
<tr><th>Comment:</th><td><input type="text" name="comment" /></td></tr>
276
Just like the ``initial`` parameter to ``Field``, these values are only
277
displayed for unbound forms, and they're not used as fallback values if a
278
particular value isn't provided.
280
Finally, note that if a ``Field`` defines ``initial`` *and* you include
281
``initial`` when instantiating the ``Form``, then the latter ``initial`` will
282
have precedence. In this example, ``initial`` is provided both at the field
283
level and at the form instance level, and the latter gets precedence::
285
>>> class CommentForm(forms.Form):
286
... name = forms.CharField(initial='class')
287
... url = forms.URLField()
288
... comment = forms.CharField()
289
>>> f = CommentForm(initial={'name': 'instance'}, auto_id=False)
291
<tr><th>Name:</th><td><input type="text" name="name" value="instance" /></td></tr>
292
<tr><th>Url:</th><td><input type="text" name="url" /></td></tr>
293
<tr><th>Comment:</th><td><input type="text" name="comment" /></td></tr>
296
260
Built-in ``Field`` classes
297
261
--------------------------
311
275
* Default widget: ``CheckboxInput``
312
276
* Empty value: ``False``
313
277
* Normalizes to: A Python ``True`` or ``False`` value.
314
* Validates that the check box is checked (i.e. the value is ``True``) if
278
* Validates that the value is ``True`` (e.g. the check box is checked) if
315
279
the field has ``required=True``.
316
280
* Error message keys: ``required``
325
289
Since all ``Field`` subclasses have ``required=True`` by default, the
326
validation condition here is important. If you want to include a checkbox
327
in your form that can be either checked or unchecked, you must remember to
328
pass in ``required=False`` when creating the ``BooleanField``.
290
validation condition here is important. If you want to include a boolean
291
in your form that can be either ``True`` or ``False`` (e.g. a checked or
292
unchecked checkbox), you must remember to pass in ``required=False`` when
293
creating the ``BooleanField``.
419
384
'%B %d %Y', '%B %d, %Y', # 'October 25 2006', 'October 25, 2006'
420
385
'%d %B %Y', '%d %B, %Y', # '25 October 2006', '25 October, 2006'
387
.. versionchanged:: 1.1
388
The ``DateField`` previously used a ``TextInput`` widget by default. It now
389
uses a ``DateInput`` widget.
422
391
``DateTimeField``
423
392
~~~~~~~~~~~~~~~~~
517
486
* Error message keys: ``required``, ``invalid``, ``missing``, ``empty``
519
488
To learn more about the ``UploadedFile`` object, see the :ref:`file uploads
520
documentation <topics-file-uploads>`.
489
documentation <topics-http-file-uploads>`.
522
491
When you use a ``FileField`` in a form, you must also remember to
523
:ref:`bind the file data to the form <topics-file-uploads>`.
492
:ref:`bind the file data to the form <binding-uploaded-files>`.
525
494
``FilePathField``
526
495
~~~~~~~~~~~~~~~~~
554
523
A regular expression pattern; only files with names matching this expression
555
524
will be allowed as choices.
560
* Default widget: ``TextInput``
561
* Empty value: ``None``
562
* Normalizes to: A Python float.
563
* Validates that the given value is an float. Leading and trailing
564
whitespace is allowed, as in Python's ``float()`` function.
565
* Error message keys: ``required``, ``invalid``, ``max_value``,
568
Takes two optional arguments for validation, ``max_value`` and ``min_value``.
529
* Default widget: ``TextInput``
530
* Empty value: ``None``
531
* Normalizes to: A Python float.
532
* Validates that the given value is an float. Leading and trailing
533
whitespace is allowed, as in Python's ``float()`` function.
534
* Error message keys: ``required``, ``invalid``, ``max_value``,
537
Takes two optional arguments for validation, ``max_value`` and ``min_value``.
569
538
These control the range of values permitted in the field.
739
708
.. class:: SplitDateTimeField(**kwargs)
710
* Default widget: ``SplitDateTimeWidget``
711
* Empty value: ``None``
712
* Normalizes to: A Python ``datetime.datetime`` object.
713
* Validates that the given value is a ``datetime.datetime`` or string
714
formatted in a particular datetime format.
715
* Error message keys: ``required``, ``invalid``
717
Takes two optional arguments:
719
.. attribute:: SplitDateTimeField.input_date_formats
721
A list of formats used to attempt to convert a string to a valid
722
``datetime.date`` object.
724
If no ``input_date_formats`` argument is provided, the default input formats
725
for ``DateField`` are used.
727
.. attribute:: SplitDateTimeField.input_time_formats
729
A list of formats used to attempt to convert a string to a valid
730
``datetime.time`` object.
732
If no ``input_time_formats`` argument is provided, the default input formats
733
for ``TimeField`` are used.
735
.. versionchanged:: 1.1
736
The ``SplitDateTimeField`` previously used two ``TextInput`` widgets by
737
default. The ``input_date_formats`` and ``input_time_formats`` arguments
741
740
Fields which handle relationships
742
741
---------------------------------
774
773
def label_from_instance(self, obj):
775
774
return "My Object #%i" % obj.id
776
.. attribute:: ModelChoiceField.empty_label
778
By default the ``<select>`` widget used by ``ModelChoiceField`` will have a
779
an empty choice at the top of the list. You can change the text of this label
780
(which is ``"---------"`` by default) with the ``empty_label`` attribute, or
781
you can disable the empty label entirely by setting ``empty_label`` to
784
# A custom empty label
785
field1 = forms.ModelChoiceField(queryset=..., empty_label="(Nothing)")
788
field2 = forms.ModelChoiceField(queryset=..., empty_label=None)
790
Note that if a ``ModelChoiceField`` is required and has a default
791
initial value, no empty choice is created (regardless of the value
777
794
``ModelMultipleChoiceField``
778
795
~~~~~~~~~~~~~~~~~~~~~~~~~~~~