Topic guide
Formsets
Sometimes your form class takes custom parameters, like MyArticleForm. You can pass this parameter when instantiating the formset: The form_kwargs may also depend on the specific form instance. The formset base class provides a get_form_kwargs method.
Topic guide
Working with forms
More about Django Form classesMore on fields
Whatever the data submitted with a form, once it has been successfully validated by calling is_valid() (and is_valid() has returned True), the validated form data will be in the form.cleaned_data dictionary.
Topic guide
Testing tools
Test cases features
SimpleTestCase.assertFormSetError(formset,form_index,field,errors,msg_prefix='') : Asserts that the formset raises the provided list of errors when rendered. SimpleTestCase.assertContains(response,text,count=None,status_code=200,msg_prefix='',html=False) : Asserts that a response produced the given status_code and that text appears in its content.
Reference
The Forms API
If you need to access some additional information about a form field in a template and using a subclass of Field isn’t sufficient, consider also customizing BoundField.
Reference
Middleware
Available middleware
classCsrfViewMiddleware Adds protection against Cross Site Request Forgeries by adding hidden form fields to POST forms and checking requests for the correct value. See the Cross Site Request Forgery protection documentation.
Reference
Model field reference
Field options
Field.help_text Extra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form. Note that this value is not HTML-escaped in automatically-generated forms.
Topic guide
Working with forms
In HTML, a form is a collection of elements inside <form>...</form> that allow a visitor to do things like enter text, select options, manipulate objects or controls, and so on, and then send that information back to the server.
Topic guide
Working with forms
Working with form templates
We don’t have to let Django unpack the form’s fields; we can do it manually if we like (allowing us to reorder the fields, for example).
Topic guide
Working with forms
Working with form templatesLooping over the form’s fields
If you’re manually laying out a form in a template, as opposed to relying on Django’s default form layout, you might want to treat <input type="hidden"> fields differently from non-hidden fields.
Internals
Committing code
Since Django is hosted on GitHub, patches are provided in the form of pull requests. When committing a pull request, make sure each individual commit matches the commit guidelines described below.
Internals
Django’s release process
Starting with Django 2.0, version numbers will use a loose form of semantic versioning such that each version following an LTS will bump to the next “dot zero” version. For example: 2.0, 2.1, 2.2 (LTS), 3.0, 3.1, 3.2 (LTS), etc.
Reference
The Django admin site
ModelAdmin objects
ModelAdmin.get_exclude(request,obj=None) : The get_exclude method is given the HttpRequest and the obj being edited (or None on an add form) and is expected to return a list of fields, as described in ModelAdmin.exclude.
Reference
The Forms API
More granular output
BoundField.name : The name of this field in the form: BoundField.use_fieldset : > New in Django 4.1. BoundField.widget_type : Returns the lowercased class name of the wrapped field’s widget, with any trailing input or widget removed.
Reference
Model field reference
Field types
The default form widget for this field is a NumberInput when localize is False or TextInput otherwise. NOTE: The FloatField class is sometimes mixed up with the > DecimalField class.
Reference
Model field reference
Field types
The default form widget for this field is a Textarea. If you specify a max_length attribute, it will be reflected in the Textarea widget of the auto-generated form field. However it is not enforced at the model or database level.
Topic guide
Models
Fields
help_text : Extra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form. primary_key : If True, this field is the primary key for the model.
Topic guide
Working with forms
HTML forms
GET and POST are the only HTTP methods to use when dealing with forms.
Topic guide
Creating forms from models
ModelForm
It is strongly recommended that you explicitly set all fields that should be edited in the form using the fields attribute.
FAQ
FAQ: The admin
You’ve got several options. If you want to piggyback on top of an add/change form that Django automatically generates, you can attach arbitrary JavaScript modules to the page via the model’s class Admin js parameter.
How-to
How to use Django’s CSRF protection
Edge cases
There are cases when CsrfViewMiddleware.process_view may not have run before your view is run - 404 and 500 handlers, for example - but you still need the CSRF token in a form. Solution: use requires_csrf_token()