djangodocs.org

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

Writing validators

Reference Validators

A validator is a callable that takes a value and raises a ValidationError if it doesn’t meet some criteria. Validators can be useful for reusing validation logic between different types of fields.

4.2 5.0 5.1 5.2 6.0

The template

Topic guide Working with forms Building a formBuilding a form in Django

We don’t need to do much in our name.html template: All the form’s fields and their attributes will be unpacked into HTML markup from that {{ form }} by Django’s template language.

4.2 5.0 5.1 5.2 6.0

Validating objects

Reference Model instance reference

… as a whole - Model.clean() Validate the field uniqueness - Model.validate_unique() Validate the constraints - Model.validate_constraints() All four steps are performed when you call a model’s full_clean() method. When you use a ModelForm, the call to is_valid() will perform these …

4.2 5.0 5.1 5.2 6.0

PasswordInput

Reference Widgets Built-in widgetsWidgets handling input of text

classPasswordInput[source] : - input_type: 'password' - template_name: 'django/forms/widgets/password.html' - Renders as: <input type="password" ...>

4.2 5.0 5.1 5.2 6.0

Uploading multiple files

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.

4.2 5.0 5.1 5.2 6.0

Reference The Forms API

classErrorList(initlist=None,error_class=None,renderer=None,field_id=None)[source] : By default, forms use django.forms.utils.ErrorList to format validation errors. ErrorList is a list like object where initlist is the list of errors. In addition this class has the following attributes and methods.

dev

TelInput

Reference Widgets

classTelInput[source] : - input_type: 'tel' - template_name: 'django/forms/widgets/tel.html' - Renders as: <input type="tel" ...>

dev

PasswordInput

Reference Widgets

classPasswordInput[source] : - input_type: 'password' - template_name: 'django/forms/widgets/password.html' - Renders as: <input type="password" ...>

dev

choices

Reference Model field reference

Field.choices[source] A mapping or iterable in the format described below to use as choices for this field.

dev

CharField

Reference Model field reference

The default form widget for this field is a TextInput. CharField has the following extra arguments: CharField.max_length : The maximum length (in characters) of the field. The max_length is enforced at the database level and in Django’s validation using MaxLengthValidator.

dev

Built-in forms

Topic guide Using the Django authentication system

classBaseUserCreationForm[source] : A ModelForm for creating a new user. This is the recommended base class if you need to customize the user creation form. classPasswordChangeForm[source] : A form for allowing a user to change their password.

dev

validate_max

Topic guide Formsets

If validate_max=True is passed to formset_factory(), validation will also check that the number of forms in the data set, minus those marked for deletion, is less than or equal to max_num.

dev

validate_min

Topic guide Formsets

If validate_min=True is passed to formset_factory(), validation will also check that the number of forms in the data set, minus those marked for deletion, is greater than or equal to min_num.

dev

The template

Topic guide Working with forms

We don’t need to do much in our name.html template: All the form’s fields and their attributes will be unpacked into HTML markup from that {{ form }} by Django’s template language.

dev

Topic guide Creating forms from models

When overriding methods on InlineFormSet, you should subclass BaseInlineFormSet rather than BaseModelFormSet. For example, if you want to override clean(): See also Overriding clean() on a ModelFormSet. Then when you create your inline formset, pass in the optional argument formset:

dev