Reference
The Forms API
More granular output
BoundField.legend_tag(contents=None,attrs=None,label_suffix=None)[source] : Calls label_tag() with tag='legend' to render the label with <legend> tags. This is useful when rendering radio and multiple checkbox widgets where <legend> may be more appropriate than a <label>.
Reference
Model forms
Model form Meta API
ModelFormOptions.exclude : A tuple or list of model field names to be excluded from the form.
Reference
Model forms
Model form Meta API
ModelFormOptions.field_classes : A dictionary that maps a model field name to a Field class, which overrides the form_class used in the model field’s Field.formfield() method.
Reference
Model forms
Model form Meta API
ModelFormOptions.fields : A tuple or list of model field names to be included in the form. The value '__all__' can be used to specify that all fields should be included.
Reference
Model forms
Model form Meta API
ModelFormOptions.localized_fields : A tuple or list of model field names to be localized. The value '__all__' can be used to specify that all fields should be localized.
Reference
Model forms
Model form factory functions
modelform_factory(model,form=ModelForm,fields=None,exclude=None,formfield_callback=None,widgets=None,localized_fields=None,labels=None,help_texts=None,error_messages=None,field_classes=None)[source] : Returns a ModelForm class for the given model. You can optionally pass a form argument to use as a starting point for constructing the ModelForm.
Reference
Model forms
Model form factory functions
modelformset_factory(model,form=ModelForm,formfield_callback=None,formset=BaseModelFormSet,extra=1,can_delete=False,can_order=False,max_num=None,fields=None,exclude=None,widgets=None,validate_max=False,localized_fields=None,labels=None,help_texts=None,error_messages=None,min_num=None,validate_min=False,field_classes=None,absolute_max=None,can_delete_extra=True,renderer=None,edit_only=False)[source] : Returns a FormSet class for the given model class.
Reference
Widgets
Whenever you specify a field on a form, Django will use a default widget that is appropriate to the type of data that is to be displayed.
Reference
Built-in template tags and filters
Built-in tag referenceif
All of the above can be combined to form complex expressions. For such expressions, it can be important to know how the operators are grouped when the expression is evaluated - that is, the precedence rules.
Topic guide
Formsets
Sometimes your form class takes custom parameters, like MyArticleForm. You can pass this parameter when instantiating the formset: The form_kwargs may also depend on the specific form instance. The formset base class provides a get_form_kwargs method.
Topic guide
Working with forms
More about Django Form classesMore on fields
Whatever the data submitted with a form, once it has been successfully validated by calling is_valid() (and is_valid() has returned True), the validated form data will be in the form.cleaned_data dictionary.
Topic guide
Testing tools
Test cases features
SimpleTestCase.assertFormSetError(formset,form_index,field,errors,msg_prefix='')[source] : Asserts that the formset raises the provided list of errors when rendered. SimpleTestCase.assertContains(response,text,count=None,status_code=200,msg_prefix='',html=False)[source] : Asserts that a response produced the given status_code and that text appears in its content.
Reference
GeoDjango Forms API
Form widgets
OpenLayersWidget classOpenLayersWidget[source] : This is the default widget used by all GeoDjango form fields. template_name is gis/openlayers.html. OSMWidget classOSMWidget[source] : This widget uses an OpenStreetMap base layer to display geographic objects on. Attributes are:
Reference
Model field reference
Field options
Field.help_text Extra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form. Note that this value is not HTML-escaped in automatically-generated forms.
Topic guide
Working with forms
In HTML, a form is a collection of elements inside <form>...</form> that allow a visitor to do things like enter text, select options, manipulate objects or controls, and so on, and then send that information back to the server.
Topic guide
Working with forms
Working with form templates
Each field is available as an attribute of the form, using {{ form.name_of_field }} in a template.
Topic guide
Working with forms
Working with form templates
However, it can also be directly accessed from the field attribute on the form. For example: Complete <label> elements can also be generated using the label_tag(). For example:
Topic guide
Working with forms
Working with form templatesLooping over the form’s fields
If you’re manually laying out a form in a template, as opposed to relying on Django’s default form layout, you might want to treat <input type="hidden"> fields differently from non-hidden fields.
Topic guide
File Uploads
Basic file uploads
If you want to upload multiple files using one form field, create a subclass of the field’s widget and set its allow_multiple_selected class attribute to True.
Internals
Committing code
Since Django is hosted on GitHub, patches are provided in the form of pull requests. When committing a pull request, make sure each individual commit matches the commit guidelines described below.