djangodocs.org

706 sections in Django 6.0 Search all versions →

Widget attributes

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).

Widget classes

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:

Outputting forms as HTML

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.

Default rendering

Reference The Forms API Outputting forms as HTML

The default rendering when you print a form uses the following methods and attributes.

Methods of BoundField

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>.

exclude

Reference Model forms Model form Meta API

ModelFormOptions.exclude : A tuple or list of model field names to be excluded from the form.

field_classes

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.

fields

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.

localized_fields

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.

modelform_factory

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.

modelformset_factory

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.

Specifying widgets

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.

Complex expressions

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.

Field data

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.

Assertions

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.

help_text

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.

HTML 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.