Reference
The Forms API
Outputting forms as HTML
If you render a bound Form object, the act of rendering will automatically run the form’s validation if it hasn’t already happened, and the HTML output will include the validation errors as a <ul class="errorlist">.
Release notes
Django version 0.96 release notes
What’s new in 0.96?
django.newforms is Django’s new form-handling library. It’s a replacement for django.forms, the old form/manipulator/validation framework.
Reference
Since the field validation methods have been run by the time clean() is called, you also have access to the form’s errors attribute which contains all the errors raised by cleaning of individual fields.
Reference
Form and field validation
… coerce variables into the message; use placeholders and the params argument of the constructor: Use mapping keys instead of positional formatting. This enables putting the variables in any order or omitting them altogether when rewriting the message: Wrap the message …
Reference
Django Exceptions
Django Core Exceptions
exceptionValidationError[source] : The ValidationError exception is raised when data fails form or model field validation. For more information about validation, see Form and Field Validation, Model Field Validation and the Validator Reference.
Reference
Form and field validation
The previous sections explained how validation works in general for forms.
Reference
Form and field validation
Raising ValidationError
If you detect multiple errors during a cleaning method and wish to signal all of them to the form submitter, it is possible to pass a list of errors to the ValidationError constructor.
Reference
Form and field validation
Using validation in practice
Django’s form (and model) fields support use of utility functions and classes known as validators.
Reference
Form and field validation
Using validation in practice
Let’s first create a custom form field that validates its input is a string containing comma-separated email addresses.
Reference
Form and field validation
Using validation in practice
Continuing on from the previous example, suppose that in our ContactForm, we want to make sure that the recipients field always contains the address "[email protected]".
Reference
Form and field validation
Using validation in practice
Suppose we add another requirement to our contact form: if the cc_myself field is True, the subject must contain the word "help".
Reference
The Forms API
Form.clean() Implement a clean() method on your Form when you must add custom validation for fields that are interdependent. See Cleaning and validating fields that depend on each other for example usage.
Reference
The Forms API
Form.cleaned_data Each field in a Form class is responsible not only for validating data, but also for “cleaning” it – normalizing it to a consistent format.
Reference
Detailed form API reference. For introductory material, see the Working with forms topic guide.
Topic guide
NOTE: This document provides an introduction to the basics of web forms and how > they are handled in Django.
Topic guide
Working with forms
This covers the basics, but forms can do a whole lot more: Formsets Using initial data with a formset Limiting the maximum number of forms Limiting the maximum number of instantiated forms Formset validation Validating the number of forms in …
… template system Removing hardcoded URLs in templates Namespacing URL names Writing your first Django app, part 4 Write a minimal form Use generic views: Less code is better Writing your first Django app, part 5 Introducing automated testing Basic testing …
Topic guide
Formsets
Validation with a formset is almost identical to a regular Form.
Topic guide
Formsets
Formset validation
A formset has a clean method similar to the one on a Form class.
Reference
classField[source] When you create a Form class, the most important part is defining the fields of the form. Each field has custom validation logic, along with a few other hooks.