Topic guide Creating forms from models
As with basic forms, you can extend and reuse ModelForm classes by inheriting them.
1293 sections across all versions Narrow to Django 6.0 (current) →
Topic guide Creating forms from models
As with basic forms, you can extend and reuse ModelForm classes by inheriting them.
Topic guide Creating forms from models ModelForm
As with basic forms, you can extend and reuse ModelForm classes by inheriting them.
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.
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
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
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
At the heart of this system of components is Django’s 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).
Topic guide Working with forms
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
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
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 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.