djangodocs.org

1305 sections across all versions Narrow to Django 6.0 (current) →

Output styles

Reference The Forms API Outputting forms as HTML

The recommended approach for changing form output style is to set a custom form template either site-wide, per-form, or per-instance. See Reusable form templates for examples.

4.2 5.0 5.1 5.2 6.0

Using mixins

Topic guide Introduction to class-based views

Mixins are a form of multiple inheritance where behaviors and attributes of multiple parent classes can be combined. For example, in the generic class-based views there is a mixin called TemplateResponseMixin whose primary purpose is to define the method render_to_response().

4.2 5.0 5.1 5.2 6.0

More on fields

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.

4.2 5.0 5.1 5.2 6.0

Basic file uploads

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.

4.2 5.0 5.1 5.2 6.0

Models

Reference System check framework

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

dev

Geometry Lookups

Reference GeoDjango Database API

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 …

dev

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.

dev

Methods of BoundField

Reference The Forms API

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

dev

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.

dev

Complex expressions

Reference Built-in template tags and filters

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.

dev

Topic guide Formsets

You may have noticed the additional data (form-TOTAL_FORMS, form-INITIAL_FORMS) that was required in the formset’s data above. This data is required for the ManagementForm.

dev

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.

dev

Field data

Topic guide Working with forms

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.

dev

Assertions

Topic guide Testing tools

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 an HttpResponse produced the given status_code and that text appears in its content.

dev

Models

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

4.2 5.0 5.1 5.2 6.0

Geometry Lookups

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 …

4.2 5.0 5.1 5.2 6.0