Topic guide
Working with forms
More about Django Form classes
Consider a more useful form than our minimal example above, which we could use to implement “contact me” functionality on a personal website: forms.py Our earlier form used a single field, your_name, a CharField.
Topic guide
File Uploads
Consider a form containing a FileField: forms.py A view handling this form will receive the file data in request.FILES, which is a dictionary containing a key for each FileField (or ImageField, or other FileField subclass) in the form.
Reference
System check framework
Core system checks
models.E001: <swappable> is not of the form app_label.app_name. models.E002: <SETTING> references <model>, which has not been installed, or is abstract. models.E003: The model has two identical many-to-many relations through the intermediate model <app_label>.<model>.
Reference
GeoDjango Database API
Spatial Lookups
Geographic queries with geometries take the following general form (assuming the Zipcode model used in the GeoDjango Model API): For example: In this case, poly is the geographic field, contains is the spatial lookup type, pnt is the parameter (which …
Reference
GeoDjango Forms API
Form widgets
BaseGeometryWidget.base_layer : > New in Django 6.0. BaseGeometryWidget.geom_type : The OpenGIS geometry type, generally set by the form field. BaseGeometryWidget.map_srid : SRID code used by the map (default is 4326).
Reference
GeoDjango Forms API
Form widgets
OpenLayersWidget classOpenLayersWidget[source] : This is the default widget used by all GeoDjango form fields. Attributes are: OSMWidget classOSMWidget[source] : This widget specialized OpenLayersWidget and uses an OpenStreetMap base layer to display geographic objects on. Attributes are:
Reference
The Forms API
Using forms to validate data
It’s meaningless to validate a form with no data, but, for the record, here’s what happens with unbound forms:
Reference
The Forms API
The second task of a Form object is to render itself as HTML. To do so, print it: If the form is bound to data, the HTML output will include that data appropriately.
Reference
The Forms API
Outputting forms as HTML
The default rendering when you print a form uses the following methods and attributes.
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.
5.2
6.0
dev
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.
5.2
6.0
dev
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.
5.2
6.0
dev
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.
5.2
6.0
dev
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.
5.2
6.0
dev
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.
Reference
Model field reference
Field types
classBooleanField(**options)[source] A true/false field. The default form widget for this field is CheckboxInput, or NullBooleanSelect if null=True. The default value of BooleanField is None when Field.default isn’t defined.