Topic guide
Creating forms from models
ModelForm
As with basic forms, you can extend and reuse ModelForms by inheriting them. This is useful if you need to declare extra fields or extra methods on a parent class for use in a number of forms derived from models.
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.
Reference
Cross Site Request Forgery protection
Frequently Asked Questions
For security reasons, CSRF tokens are rotated each time a user logs in. Any page with a form generated before a login will have an old, invalid CSRF token and need to be reloaded.
Reference
The Forms API
Outputting forms as HTML
By default, auto_id is set to the string 'id_%s'. Form.label_suffix A translatable string (defaults to a colon (:) in English) that will be appended after any label name when a form is rendered.
Topic guide
Working with forms
Forms in Django
At the heart of this system of components is Django’s 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 }} …
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.
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 …
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.
Reference
Form fields
Built-in Field classes
classBooleanField(**kwargs) : - 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.
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
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.
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.
Topic guide
Using the Django authentication system
Authentication in web requests
classSetPasswordForm : A form that lets a user change their password without entering the old password. classUserChangeForm : A form used in the admin interface to change a user’s information and permissions. classBaseUserCreationForm : > New in Django 4.2.
Topic guide
Creating forms from models
ModelFormValidation on a ModelForm
Error messages defined at the form field level or at the form Meta level always take precedence over the error messages defined at the model field level.
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.
Topic guide
Creating forms from models
ModelFormValidation on a ModelForm
You can override the clean() method on a model form to provide additional validation in the same way you can on a normal form.
Reference
PostgreSQL specific form fields and widgets
Fields
classSplitArrayField(base_field,size,remove_trailing_nulls=False) : This field handles arrays by reproducing the underlying field a fixed number of times.