djangodocs.org

694 sections in Django 5.2 Search all versions →

Integrating validation

Topic guide Password management in Django Password validation

There are a few functions in django.contrib.auth.password_validation that you can call from your own forms or other code to integrate password validation.

The Form class

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

We already know what we want our HTML form to look like. Our starting point for it in Django is this: forms.py This defines a Form class with a single field (your_name).

Topic guide Working with forms Working with form templatesRendering fields manually

The price of this flexibility is a bit more work. Until now we haven’t had to worry about how to display form errors, because that’s taken care of for us.

Topic guide Working with forms Working with form templates

If you’re using the same HTML for each of your form fields, you can reduce duplicate code by looping through each field in turn using a {% for %} loop: Useful attributes on {{ field }} include: {{ field.errors }} …

Changing the form

Topic guide Creating forms from models Model formsets

By default, when you use modelformset_factory, a model form will be created using modelform_factory(). Often, it can be useful to specify a custom model form.

required

Reference Form fields Core field arguments

… if you pass an empty value – either None or the empty string ("") – then clean() will raise a ValidationError exception: To specify that a field is not required, pass required=False to the Field constructor: If a Field has …

initial

Reference Form fields Core field arguments

Field.initial The initial argument lets you specify the initial value to use when rendering this Field in an unbound Form. To specify dynamic initial data, see the Form.initial parameter.

BooleanField

Reference Form fields Built-in Field classes

classBooleanField(**kwargs)[source] : - Default widget: CheckboxInput - Empty value: False - Normalizes to: A Python True or False value. - Validates that the value is True (e.g. the check box is checked) if the field has required=True.

FileField

Reference Form fields Built-in Field classes

- Can validate that non-empty file data has been bound to the form. - Error message keys: required, invalid, missing, empty, max_length

Model forms

Topic guide Form handling with class-based views

Generic views really shine when working with models. These generic views will automatically create a ModelForm, so long as they can work out which model class to use: If the model attribute is given, that model class will be used.

NON_FIELD_ERRORS

Reference Django Exceptions Django Core ExceptionsValidationError

NON_FIELD_ERRORS ValidationErrors that don’t belong to a particular field in a form or model are classified as NON_FIELD_ERRORS. This constant is used as a key in dictionaries that otherwise map fields to their respective list of errors.

Bound and unbound forms

Reference The Forms API

A Form instance is either bound to a set of data, or unbound. If it’s bound to a set of data, it’s capable of validating that data and rendering the form as HTML with the data displayed in the HTML.

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.