djangodocs.org

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

Integrating validation

Topic guide Password management

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.

dev

The Form class

Topic guide Working with forms

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

dev

Topic guide Working with forms

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 }} …

dev

Changing the form

Topic guide Creating forms from models

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.

dev

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

4.2 5.0 5.1 5.2 6.0

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 }} …

4.2 5.0 5.1 5.2 6.0

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.