djangodocs.org

717 sections in Django dev Search all versions →

Reference

Form validation happens when the data is cleaned. If you want to customize this process, there are various places to make changes, each one serving a different purpose. Three types of cleaning methods are run during form processing.

Topic guide Creating forms from models ModelForm

There are two main steps involved in validating a ModelForm: Validating the form Validating the model instance Just like normal form validation, model form validation is triggered implicitly when calling is_valid() or accessing the errors attribute and explicitly when calling …

Reference The Django admin site

The admin uses ModelForm. To add custom validation, provide your own ModelForm: Then configure the ModelAdmin to use the form: Or configure an InlineModelAdmin to use the form: It is important that you use a ModelForm; otherwise things can break.

How validators are run

Reference Validators

See the form validation for more information on how validators are run in forms, and Validating objects for how they’re run in models.

Reference Form fields

Upon form validation, these fields will place either one model object (in the case of ModelChoiceField) or multiple model objects (in the case of ModelMultipleChoiceField) into the cleaned_data dictionary of the form.

blank

Reference Model field reference Field options

Note that this is different than null. null is purely database-related, whereas blank is validation-related. If a field has blank=True, form validation will allow entry of an empty value. If a field has blank=False, the field will be required.

Testing in Django

Topic guide

Testing a web application is a complex task, because a web application is made of several layers of logic – from HTTP-level request handling, to form validation and processing, to template rendering.

Field options

Topic guide Models Fields

Default is False. choices : A sequence of 2-value tuples, a mapping, an enumeration type, or a callable (that expects no arguments and returns any of the previous formats), to use as choices for this field.

Release notes Django 1.2 release notes Backwards-incompatible changes in 1.2

Much of the validation work for ModelForms has been moved down to the model level. As a result, the first time you call ModelForm.is_valid(), access ModelForm.errors or otherwise trigger form validation, your model will be cleaned in-place.

Overview

Release notes Django 1.2 release notes

Model validation inspired by Django’s form validation. Vastly improved protection against Cross-Site Request Forgery (CSRF). A new user “messages” framework with support for cookie- and session-based message for both anonymous and authenticated users.

Release notes Django 1.3 release notes Features deprecated in 1.3

This release refactors the admin’s login mechanism to use a subclass of the AuthenticationForm instead of a manual form validation. The previously undocumented method 'django.contrib.admin.sites.AdminSite.display_login_form' has been removed in favor of a new login_form attribute.

Miscellaneous

Release notes Django 1.11 release notes Features deprecated in 1.11

If no items in the feed have a pubdate or updateddate attribute, SyndicationFeed.latest_post_date() now returns the current UTC date/time, instead of a datetime without any timezone information. CSRF failures are logged to the django.security.csrf logger instead of django.request.

Miscellaneous

Release notes Django 1.6 release notes Backwards incompatible changes in 1.6

Form field’s error_messages that contain a placeholder should now always use a named placeholder ("Value '%(value)s' is too big" instead of "Value '%s' is too big"). See the corresponding field documentation for details about the names of the placeholders.

Bugfixes

Release notes Django 1.9.5 release notes

The forms in contrib.auth no longer strip trailing and leading whitespace from the password fields (#26334). The change requires users who set their password to something with such whitespace after a site updated to Django 1.9 to reset their password.

FormView

Reference Generic editing views

classdjango.views.generic.edit.FormView : A view that displays a form. On error, redisplays the form with validation errors; on success, redirects to a new URL. classdjango.views.generic.edit.BaseFormView : A base view for displaying a form.

CreateView

Reference Generic editing views

classdjango.views.generic.edit.CreateView : A view that displays a form for creating an object, redisplaying the form with validation errors (if there are any) and saving the object. classdjango.views.generic.edit.BaseCreateView : A base view for creating a new object instance.

UpdateView

Reference Generic editing views

classdjango.views.generic.edit.UpdateView : A view that displays a form for editing an existing object, redisplaying the form with validation errors (if there are any) and saving changes to the object.