djangodocs.org

711 sections in Django dev Search all versions →

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.

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

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 …

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.

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

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

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

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.

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

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

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.

max_geom_collections

Reference GeoDjango Model API

GeometryField.max_geom_collections This option is forwarded to the form field generated for this model field, bounding the geometry collection nesting depth in submitted WKB/WKT inputs before raising ValueError.

help_text

Reference Model field reference

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.

Topic guide Working with forms

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.

Uploading multiple files

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

Handling pull requests

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.