{"title":"Form and field validation","version":"2.1","locale":"zh-hans","docname":"ref/forms/validation","url":"/zh-hans/2.1/ref/forms/validation/","canonical":"https://djangodocs.dev/zh-hans/2.1/ref/forms/validation/","summary":"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…","html":"<h1>Form and field validation<a class=\"heading-anchor\" href=\"#form-and-field-validation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Form validation happens when the data is cleaned. If you want to customize\nthis process, there are various places to make changes, each one serving a\ndifferent purpose. Three types of cleaning methods are run during form\nprocessing. These are normally executed when you call the <code class=\"docutils literal notranslate\"><span class=\"pre\">is_valid()</span></code>\nmethod on a form. There are other things that can also trigger cleaning and\nvalidation (accessing the <code class=\"docutils literal notranslate\"><span class=\"pre\">errors</span></code> attribute or calling <code class=\"docutils literal notranslate\"><span class=\"pre\">full_clean()</span></code>\ndirectly), but normally they won't be needed.</p>\n<p>In general, any cleaning method can raise <code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code> if there is a\nproblem with the data it is processing, passing the relevant information to\nthe <code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code> constructor. <a class=\"reference internal\" href=\"#raising-validation-error\"><span class=\"std std-ref\">See below</span></a>\nfor the best practice in raising <code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code>. If no <code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code>\nis raised, the method should return the cleaned (normalized) data as a Python\nobject.</p>\n<p>Most validation can be done using <a class=\"reference internal\" href=\"#validators\">validators</a> - simple helpers that can be\nreused easily. Validators are simple functions (or callables) that take a single\nargument and raise <code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code> on invalid input. Validators are run\nafter the field's <code class=\"docutils literal notranslate\"><span class=\"pre\">to_python</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">validate</span></code> methods have been called.</p>\n<p>Validation of a form is split into several steps, which can be customized or\noverridden:</p>\n<ul>\n<li><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">to_python()</span></code> method on a <code class=\"docutils literal notranslate\"><span class=\"pre\">Field</span></code> is the first step in every\nvalidation. It coerces the value to a correct datatype and raises\n<code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code> if that is not possible. This method accepts the raw\nvalue from the widget and returns the converted value. For example, a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">FloatField</span></code> will turn the data into a Python <code class=\"docutils literal notranslate\"><span class=\"pre\">float</span></code> or raise a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code>.</p></li>\n<li><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">validate()</span></code> method on a <code class=\"docutils literal notranslate\"><span class=\"pre\">Field</span></code> handles field-specific validation\nthat is not suitable for a validator. It takes a value that has been\ncoerced to a correct datatype and raises <code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code> on any error.\nThis method does not return anything and shouldn't alter the value. You\nshould override it to handle validation logic that you can't or don't\nwant to put in a validator.</p></li>\n<li><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">run_validators()</span></code> method on a <code class=\"docutils literal notranslate\"><span class=\"pre\">Field</span></code> runs all of the field's\nvalidators and aggregates all the errors into a single\n<code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code>. You shouldn't need to override this method.</p></li>\n<li><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">clean()</span></code> method on a <code class=\"docutils literal notranslate\"><span class=\"pre\">Field</span></code> subclass is responsible for running\n<code class=\"docutils literal notranslate\"><span class=\"pre\">to_python()</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">validate()</span></code>, and <code class=\"docutils literal notranslate\"><span class=\"pre\">run_validators()</span></code> in the correct\norder and propagating their errors. If, at any time, any of the methods\nraise <code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code>, the validation stops and that error is raised.\nThis method returns the clean data, which is then inserted into the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code> dictionary of the form.</p></li>\n<li><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">clean_&lt;fieldname&gt;()</span></code> method is called on a form subclass -- where\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;fieldname&gt;</span></code> is replaced with the name of the form field attribute.\nThis method does any cleaning that is specific to that particular\nattribute, unrelated to the type of field that it is. This method is not\npassed any parameters. You will need to look up the value of the field\nin <code class=\"docutils literal notranslate\"><span class=\"pre\">self.cleaned_data</span></code> and remember that it will be a Python object\nat this point, not the original string submitted in the form (it will be\nin <code class=\"docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code> because the general field <code class=\"docutils literal notranslate\"><span class=\"pre\">clean()</span></code> method, above,\nhas already cleaned the data once).</p>\n<p>For example, if you wanted to validate that the contents of a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">CharField</span></code> called <code class=\"docutils literal notranslate\"><span class=\"pre\">serialnumber</span></code> was unique,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">clean_serialnumber()</span></code> would be the right place to do this. You don't\nneed a specific field (it's just a <code class=\"docutils literal notranslate\"><span class=\"pre\">CharField</span></code>), but you want a\nformfield-specific piece of validation and, possibly,\ncleaning/normalizing the data.</p>\n<p>The return value of this method replaces the existing value in\n<code class=\"docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code>, so it must be the field's value from <code class=\"docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code> (even\nif this method didn't change it) or a new cleaned value.</p>\n</li>\n<li><p>The form subclass's <code class=\"docutils literal notranslate\"><span class=\"pre\">clean()</span></code> method can perform validation that requires\naccess to multiple form fields. This is where you might put in checks such as\n&quot;if field <code class=\"docutils literal notranslate\"><span class=\"pre\">A</span></code> is supplied, field <code class=\"docutils literal notranslate\"><span class=\"pre\">B</span></code> must contain a valid email address&quot;.\nThis method can return a completely different dictionary if it wishes, which\nwill be used as the <code class=\"docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code>.</p>\n<p>Since the field validation methods have been run by the time <code class=\"docutils literal notranslate\"><span class=\"pre\">clean()</span></code> is\ncalled, you also have access to the form's <code class=\"docutils literal notranslate\"><span class=\"pre\">errors</span></code> attribute which\ncontains all the errors raised by cleaning of individual fields.</p>\n<p>Note that any errors raised by your <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/forms/api/#django.forms.Form.clean\" title=\"django.forms.Form.clean\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Form.clean()</span></code></a> override will not\nbe associated with any field in particular. They go into a special\n&quot;field&quot; (called <code class=\"docutils literal notranslate\"><span class=\"pre\">__all__</span></code>), which you can access via the\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/forms/api/#django.forms.Form.non_field_errors\" title=\"django.forms.Form.non_field_errors\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">non_field_errors()</span></code></a> method if you need to. If you\nwant to attach errors to a specific field in the form, you need to call\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/forms/api/#django.forms.Form.add_error\" title=\"django.forms.Form.add_error\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">add_error()</span></code></a>.</p>\n<p>Also note that there are special considerations when overriding\nthe <code class=\"docutils literal notranslate\"><span class=\"pre\">clean()</span></code> method of a <code class=\"docutils literal notranslate\"><span class=\"pre\">ModelForm</span></code> subclass. (see the\n<a class=\"reference internal\" href=\"/zh-hans/2.1/topics/forms/modelforms/#overriding-modelform-clean-method\"><span class=\"std std-ref\">ModelForm documentation</span></a> for more information)</p>\n</li>\n</ul>\n<p>These methods are run in the order given above, one field at a time.  That is,\nfor each field in the form (in the order they are declared in the form\ndefinition), the <code class=\"docutils literal notranslate\"><span class=\"pre\">Field.clean()</span></code> method (or its override) is run, then\n<code class=\"docutils literal notranslate\"><span class=\"pre\">clean_&lt;fieldname&gt;()</span></code>. Finally, once those two methods are run for every\nfield, the <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/forms/api/#django.forms.Form.clean\" title=\"django.forms.Form.clean\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Form.clean()</span></code></a> method, or its override, is executed whether\nor not the previous methods have raised errors.</p>\n<p>Examples of each of these methods are provided below.</p>\n<p>As mentioned, any of these methods can raise a <code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code>. For any\nfield, if the <code class=\"docutils literal notranslate\"><span class=\"pre\">Field.clean()</span></code> method raises a <code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code>, any\nfield-specific cleaning method is not called. However, the cleaning methods\nfor all remaining fields are still executed.</p>\n<section id=\"raising-validationerror\">\n<span id=\"raising-validation-error\"></span><h2>Raising <code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code><a class=\"heading-anchor\" href=\"#raising-validationerror\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>In order to make error messages flexible and easy to override, consider the\nfollowing guidelines:</p>\n<ul>\n<li><p>Provide a descriptive error <code class=\"docutils literal notranslate\"><span class=\"pre\">code</span></code> to the constructor:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"c1\"># Good</span>\n<span class=\"n\">ValidationError</span><span class=\"p\">(</span><span class=\"n\">_</span><span class=\"p\">(</span><span class=\"s1\">&#39;Invalid value&#39;</span><span class=\"p\">),</span> <span class=\"n\">code</span><span class=\"o\">=</span><span class=\"s1\">&#39;invalid&#39;</span><span class=\"p\">)</span>\n\n<span class=\"c1\"># Bad</span>\n<span class=\"n\">ValidationError</span><span class=\"p\">(</span><span class=\"n\">_</span><span class=\"p\">(</span><span class=\"s1\">&#39;Invalid value&#39;</span><span class=\"p\">))</span>\n</code></pre></div>\n</li>\n<li><p>Don't coerce variables into the message; use placeholders and the <code class=\"docutils literal notranslate\"><span class=\"pre\">params</span></code>\nargument of the constructor:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"c1\"># Good</span>\n<span class=\"n\">ValidationError</span><span class=\"p\">(</span>\n    <span class=\"n\">_</span><span class=\"p\">(</span><span class=\"s1\">&#39;Invalid value: </span><span class=\"si\">%(value)s</span><span class=\"s1\">&#39;</span><span class=\"p\">),</span>\n    <span class=\"n\">params</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s1\">&#39;value&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;42&#39;</span><span class=\"p\">},</span>\n<span class=\"p\">)</span>\n\n<span class=\"c1\"># Bad</span>\n<span class=\"n\">ValidationError</span><span class=\"p\">(</span><span class=\"n\">_</span><span class=\"p\">(</span><span class=\"s1\">&#39;Invalid value: </span><span class=\"si\">%s</span><span class=\"s1\">&#39;</span><span class=\"p\">)</span> <span class=\"o\">%</span> <span class=\"n\">value</span><span class=\"p\">)</span>\n</code></pre></div>\n</li>\n<li><p>Use mapping keys instead of positional formatting. This enables putting\nthe variables in any order or omitting them altogether when rewriting the\nmessage:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"c1\"># Good</span>\n<span class=\"n\">ValidationError</span><span class=\"p\">(</span>\n    <span class=\"n\">_</span><span class=\"p\">(</span><span class=\"s1\">&#39;Invalid value: </span><span class=\"si\">%(value)s</span><span class=\"s1\">&#39;</span><span class=\"p\">),</span>\n    <span class=\"n\">params</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s1\">&#39;value&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;42&#39;</span><span class=\"p\">},</span>\n<span class=\"p\">)</span>\n\n<span class=\"c1\"># Bad</span>\n<span class=\"n\">ValidationError</span><span class=\"p\">(</span>\n    <span class=\"n\">_</span><span class=\"p\">(</span><span class=\"s1\">&#39;Invalid value: </span><span class=\"si\">%s</span><span class=\"s1\">&#39;</span><span class=\"p\">),</span>\n    <span class=\"n\">params</span><span class=\"o\">=</span><span class=\"p\">(</span><span class=\"s1\">&#39;42&#39;</span><span class=\"p\">,),</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n</li>\n<li><p>Wrap the message with <code class=\"docutils literal notranslate\"><span class=\"pre\">gettext</span></code> to enable translation:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"c1\"># Good</span>\n<span class=\"n\">ValidationError</span><span class=\"p\">(</span><span class=\"n\">_</span><span class=\"p\">(</span><span class=\"s1\">&#39;Invalid value&#39;</span><span class=\"p\">))</span>\n\n<span class=\"c1\"># Bad</span>\n<span class=\"n\">ValidationError</span><span class=\"p\">(</span><span class=\"s1\">&#39;Invalid value&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n</li>\n</ul>\n<p>Putting it all together:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">raise</span> <span class=\"n\">ValidationError</span><span class=\"p\">(</span>\n    <span class=\"n\">_</span><span class=\"p\">(</span><span class=\"s1\">&#39;Invalid value: </span><span class=\"si\">%(value)s</span><span class=\"s1\">&#39;</span><span class=\"p\">),</span>\n    <span class=\"n\">code</span><span class=\"o\">=</span><span class=\"s1\">&#39;invalid&#39;</span><span class=\"p\">,</span>\n    <span class=\"n\">params</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s1\">&#39;value&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;42&#39;</span><span class=\"p\">},</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p>Following these guidelines is particularly necessary if you write reusable\nforms, form fields, and model fields.</p>\n<p>While not recommended, if you are at the end of the validation chain\n(i.e. your form <code class=\"docutils literal notranslate\"><span class=\"pre\">clean()</span></code> method) and you know you will <em>never</em> need\nto override your error message you can still opt for the less verbose:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">ValidationError</span><span class=\"p\">(</span><span class=\"n\">_</span><span class=\"p\">(</span><span class=\"s1\">&#39;Invalid value: </span><span class=\"si\">%s</span><span class=\"s1\">&#39;</span><span class=\"p\">)</span> <span class=\"o\">%</span> <span class=\"n\">value</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>The <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/forms/api/#django.forms.Form.errors.as_data\" title=\"django.forms.Form.errors.as_data\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Form.errors.as_data()</span></code></a> and\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/forms/api/#django.forms.Form.errors.as_json\" title=\"django.forms.Form.errors.as_json\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Form.errors.as_json()</span></code></a> methods\ngreatly benefit from fully featured <code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code>s (with a <code class=\"docutils literal notranslate\"><span class=\"pre\">code</span></code> name\nand a <code class=\"docutils literal notranslate\"><span class=\"pre\">params</span></code> dictionary).</p>\n<section id=\"raising-multiple-errors\">\n<h3>Raising multiple errors<a class=\"heading-anchor\" href=\"#raising-multiple-errors\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you detect multiple errors during a cleaning method and wish to signal all\nof them to the form submitter, it is possible to pass a list of errors to the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code> constructor.</p>\n<p>As above, it is recommended to pass a list of <code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code> instances\nwith <code class=\"docutils literal notranslate\"><span class=\"pre\">code</span></code>s and <code class=\"docutils literal notranslate\"><span class=\"pre\">params</span></code> but a list of strings will also work:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"c1\"># Good</span>\n<span class=\"k\">raise</span> <span class=\"n\">ValidationError</span><span class=\"p\">([</span>\n    <span class=\"n\">ValidationError</span><span class=\"p\">(</span><span class=\"n\">_</span><span class=\"p\">(</span><span class=\"s1\">&#39;Error 1&#39;</span><span class=\"p\">),</span> <span class=\"n\">code</span><span class=\"o\">=</span><span class=\"s1\">&#39;error1&#39;</span><span class=\"p\">),</span>\n    <span class=\"n\">ValidationError</span><span class=\"p\">(</span><span class=\"n\">_</span><span class=\"p\">(</span><span class=\"s1\">&#39;Error 2&#39;</span><span class=\"p\">),</span> <span class=\"n\">code</span><span class=\"o\">=</span><span class=\"s1\">&#39;error2&#39;</span><span class=\"p\">),</span>\n<span class=\"p\">])</span>\n\n<span class=\"c1\"># Bad</span>\n<span class=\"k\">raise</span> <span class=\"n\">ValidationError</span><span class=\"p\">([</span>\n    <span class=\"n\">_</span><span class=\"p\">(</span><span class=\"s1\">&#39;Error 1&#39;</span><span class=\"p\">),</span>\n    <span class=\"n\">_</span><span class=\"p\">(</span><span class=\"s1\">&#39;Error 2&#39;</span><span class=\"p\">),</span>\n<span class=\"p\">])</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"using-validation-in-practice\">\n<h2>Using validation in practice<a class=\"heading-anchor\" href=\"#using-validation-in-practice\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The previous sections explained how validation works in general for forms.\nSince it can sometimes be easier to put things into place by seeing each\nfeature in use, here are a series of small examples that use each of the\nprevious features.</p>\n<section id=\"using-validators\">\n<span id=\"validators\"></span><h3>Using validators<a class=\"heading-anchor\" href=\"#using-validators\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django's form (and model) fields support use of simple utility functions and\nclasses known as validators. A validator is merely a callable object or\nfunction that takes a value and simply returns nothing if the value is valid or\nraises a <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/exceptions/#django.core.exceptions.ValidationError\" title=\"django.core.exceptions.ValidationError\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code></a> if not. These can be\npassed to a field's constructor, via the field's <code class=\"docutils literal notranslate\"><span class=\"pre\">validators</span></code> argument, or\ndefined on the <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/forms/fields/#django.forms.Field\" title=\"django.forms.Field\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Field</span></code></a> class itself with the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">default_validators</span></code> attribute.</p>\n<p>Simple validators can be used to validate values inside the field, let's have\na look at Django's <code class=\"docutils literal notranslate\"><span class=\"pre\">SlugField</span></code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">validators</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.forms</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">CharField</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">SlugField</span><span class=\"p\">(</span><span class=\"n\">CharField</span><span class=\"p\">):</span>\n    <span class=\"n\">default_validators</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">validators</span><span class=\"o\">.</span><span class=\"n\">validate_slug</span><span class=\"p\">]</span>\n</code></pre></div>\n<p>As you can see, <code class=\"docutils literal notranslate\"><span class=\"pre\">SlugField</span></code> is just a <code class=\"docutils literal notranslate\"><span class=\"pre\">CharField</span></code> with a customized\nvalidator that validates that submitted text obeys to some character rules.\nThis can also be done on field definition so:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">slug</span> <span class=\"o\">=</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">SlugField</span><span class=\"p\">()</span>\n</code></pre></div>\n<p>is equivalent to:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">slug</span> <span class=\"o\">=</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">CharField</span><span class=\"p\">(</span><span class=\"n\">validators</span><span class=\"o\">=</span><span class=\"p\">[</span><span class=\"n\">validators</span><span class=\"o\">.</span><span class=\"n\">validate_slug</span><span class=\"p\">])</span>\n</code></pre></div>\n<p>Common cases such as validating against an email or a regular expression can be\nhandled using existing validator classes available in Django. For example,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">validators.validate_slug</span></code> is an instance of\na <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/validators/#django.core.validators.RegexValidator\" title=\"django.core.validators.RegexValidator\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RegexValidator</span></code></a> constructed with the first\nargument being the pattern: <code class=\"docutils literal notranslate\"><span class=\"pre\">^[-a-zA-Z0-9_]+$</span></code>. See the section on\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/validators/\"><span class=\"doc\">writing validators</span></a> to see a list of what is already\navailable and for an example of how to write a validator.</p>\n</section>\n<section id=\"form-field-default-cleaning\">\n<h3>Form field default cleaning<a class=\"heading-anchor\" href=\"#form-field-default-cleaning\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Let's first create a custom form field that validates its input is a string\ncontaining comma-separated email addresses. The full class looks like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">forms</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.validators</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">validate_email</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">MultiEmailField</span><span class=\"p\">(</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">Field</span><span class=\"p\">):</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">to_python</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">value</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Normalize data to a list of strings.&quot;&quot;&quot;</span>\n        <span class=\"c1\"># Return an empty list if no input was given.</span>\n        <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"n\">value</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"p\">[]</span>\n        <span class=\"k\">return</span> <span class=\"n\">value</span><span class=\"o\">.</span><span class=\"n\">split</span><span class=\"p\">(</span><span class=\"s1\">&#39;,&#39;</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">validate</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">value</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Check if value consists only of valid emails.&quot;&quot;&quot;</span>\n        <span class=\"c1\"># Use the parent&#39;s handling of required fields, etc.</span>\n        <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span><span class=\"n\">validate</span><span class=\"p\">(</span><span class=\"n\">value</span><span class=\"p\">)</span>\n        <span class=\"k\">for</span> <span class=\"n\">email</span> <span class=\"ow\">in</span> <span class=\"n\">value</span><span class=\"p\">:</span>\n            <span class=\"n\">validate_email</span><span class=\"p\">(</span><span class=\"n\">email</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Every form that uses this field will have these methods run before anything\nelse can be done with the field's data. This is cleaning that is specific to\nthis type of field, regardless of how it is subsequently used.</p>\n<p>Let's create a simple <code class=\"docutils literal notranslate\"><span class=\"pre\">ContactForm</span></code> to demonstrate how you'd use this\nfield:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">Form</span><span class=\"p\">):</span>\n    <span class=\"n\">subject</span> <span class=\"o\">=</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">CharField</span><span class=\"p\">(</span><span class=\"n\">max_length</span><span class=\"o\">=</span><span class=\"mi\">100</span><span class=\"p\">)</span>\n    <span class=\"n\">message</span> <span class=\"o\">=</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">CharField</span><span class=\"p\">()</span>\n    <span class=\"n\">sender</span> <span class=\"o\">=</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">EmailField</span><span class=\"p\">()</span>\n    <span class=\"n\">recipients</span> <span class=\"o\">=</span> <span class=\"n\">MultiEmailField</span><span class=\"p\">()</span>\n    <span class=\"n\">cc_myself</span> <span class=\"o\">=</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">BooleanField</span><span class=\"p\">(</span><span class=\"n\">required</span><span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Simply use <code class=\"docutils literal notranslate\"><span class=\"pre\">MultiEmailField</span></code> like any other form field. When the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">is_valid()</span></code> method is called on the form, the <code class=\"docutils literal notranslate\"><span class=\"pre\">MultiEmailField.clean()</span></code>\nmethod will be run as part of the cleaning process and it will, in turn, call\nthe custom <code class=\"docutils literal notranslate\"><span class=\"pre\">to_python()</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">validate()</span></code> methods.</p>\n</section>\n<section id=\"cleaning-a-specific-field-attribute\">\n<h3>Cleaning a specific field attribute<a class=\"heading-anchor\" href=\"#cleaning-a-specific-field-attribute\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Continuing on from the previous example, suppose that in our <code class=\"docutils literal notranslate\"><span class=\"pre\">ContactForm</span></code>,\nwe want to make sure that the <code class=\"docutils literal notranslate\"><span class=\"pre\">recipients</span></code> field always contains the address\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;fred&#64;example.com&quot;</span></code>. This is validation that is specific to our form, so we\ndon't want to put it into the general <code class=\"docutils literal notranslate\"><span class=\"pre\">MultiEmailField</span></code> class. Instead, we\nwrite a cleaning method that operates on the <code class=\"docutils literal notranslate\"><span class=\"pre\">recipients</span></code> field, like so:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">forms</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">Form</span><span class=\"p\">):</span>\n    <span class=\"c1\"># Everything as before.</span>\n    <span class=\"o\">...</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">clean_recipients</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"n\">data</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">cleaned_data</span><span class=\"p\">[</span><span class=\"s1\">&#39;recipients&#39;</span><span class=\"p\">]</span>\n        <span class=\"k\">if</span> <span class=\"s2\">&quot;fred@example.com&quot;</span> <span class=\"ow\">not</span> <span class=\"ow\">in</span> <span class=\"n\">data</span><span class=\"p\">:</span>\n            <span class=\"k\">raise</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">ValidationError</span><span class=\"p\">(</span><span class=\"s2\">&quot;You have forgotten about Fred!&quot;</span><span class=\"p\">)</span>\n\n        <span class=\"c1\"># Always return a value to use as the new cleaned data, even if</span>\n        <span class=\"c1\"># this method didn&#39;t change it.</span>\n        <span class=\"k\">return</span> <span class=\"n\">data</span>\n</code></pre></div>\n</section>\n<section id=\"cleaning-and-validating-fields-that-depend-on-each-other\">\n<span id=\"validating-fields-with-clean\"></span><h3>Cleaning and validating fields that depend on each other<a class=\"heading-anchor\" href=\"#cleaning-and-validating-fields-that-depend-on-each-other\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Suppose we add another requirement to our contact form: if the <code class=\"docutils literal notranslate\"><span class=\"pre\">cc_myself</span></code>\nfield is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, the <code class=\"docutils literal notranslate\"><span class=\"pre\">subject</span></code> must contain the word <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;help&quot;</span></code>. We are\nperforming validation on more than one field at a time, so the form's\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/forms/api/#django.forms.Form.clean\" title=\"django.forms.Form.clean\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">clean()</span></code></a> method is a good spot to do this. Notice that we are\ntalking about the <code class=\"docutils literal notranslate\"><span class=\"pre\">clean()</span></code> method on the form here, whereas earlier we were\nwriting a <code class=\"docutils literal notranslate\"><span class=\"pre\">clean()</span></code> method on a field. It's important to keep the field and\nform difference clear when working out where to validate things. Fields are\nsingle data points, forms are a collection of fields.</p>\n<p>By the time the form's <code class=\"docutils literal notranslate\"><span class=\"pre\">clean()</span></code> method is called, all the individual field\nclean methods will have been run (the previous two sections), so\n<code class=\"docutils literal notranslate\"><span class=\"pre\">self.cleaned_data</span></code> will be populated with any data that has survived so\nfar. So you also need to remember to allow for the fact that the fields you\nare wanting to validate might not have survived the initial individual field\nchecks.</p>\n<p>There are two ways to report any errors from this step. Probably the most\ncommon method is to display the error at the top of the form. To create such\nan error, you can raise a <code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code> from the <code class=\"docutils literal notranslate\"><span class=\"pre\">clean()</span></code> method. For\nexample:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">forms</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">Form</span><span class=\"p\">):</span>\n    <span class=\"c1\"># Everything as before.</span>\n    <span class=\"o\">...</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">clean</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"n\">cleaned_data</span> <span class=\"o\">=</span> <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span><span class=\"n\">clean</span><span class=\"p\">()</span>\n        <span class=\"n\">cc_myself</span> <span class=\"o\">=</span> <span class=\"n\">cleaned_data</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s2\">&quot;cc_myself&quot;</span><span class=\"p\">)</span>\n        <span class=\"n\">subject</span> <span class=\"o\">=</span> <span class=\"n\">cleaned_data</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">)</span>\n\n        <span class=\"k\">if</span> <span class=\"n\">cc_myself</span> <span class=\"ow\">and</span> <span class=\"n\">subject</span><span class=\"p\">:</span>\n            <span class=\"c1\"># Only do something if both fields are valid so far.</span>\n            <span class=\"k\">if</span> <span class=\"s2\">&quot;help&quot;</span> <span class=\"ow\">not</span> <span class=\"ow\">in</span> <span class=\"n\">subject</span><span class=\"p\">:</span>\n                <span class=\"k\">raise</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">ValidationError</span><span class=\"p\">(</span>\n                    <span class=\"s2\">&quot;Did not send for &#39;help&#39; in the subject despite &quot;</span>\n                    <span class=\"s2\">&quot;CC&#39;ing yourself.&quot;</span>\n                <span class=\"p\">)</span>\n</code></pre></div>\n<p>In this code, if the validation error is raised, the form will display an\nerror message at the top of the form (normally) describing the problem.</p>\n<p>The call to <code class=\"docutils literal notranslate\"><span class=\"pre\">super().clean()</span></code> in the example code ensures that any validation\nlogic in parent classes is maintained. If your form inherits another that\ndoesn't return a <code class=\"docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code> dictionary in its <code class=\"docutils literal notranslate\"><span class=\"pre\">clean()</span></code> method (doing\nso is optional), then don't assign <code class=\"docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code> to the result of the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">super()</span></code> call and use <code class=\"docutils literal notranslate\"><span class=\"pre\">self.cleaned_data</span></code> instead:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">clean</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n    <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span><span class=\"n\">clean</span><span class=\"p\">()</span>\n    <span class=\"n\">cc_myself</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">cleaned_data</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s2\">&quot;cc_myself&quot;</span><span class=\"p\">)</span>\n    <span class=\"o\">...</span>\n</code></pre></div>\n<p>The second approach for reporting validation errors might involve assigning the\nerror message to one of the fields. In this case, let's assign an error message\nto both the &quot;subject&quot; and &quot;cc_myself&quot; rows in the form display. Be careful when\ndoing this in practice, since it can lead to confusing form output. We're\nshowing what is possible here and leaving it up to you and your designers to\nwork out what works effectively in your particular situation. Our new code\n(replacing the previous sample) looks like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">forms</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">Form</span><span class=\"p\">):</span>\n    <span class=\"c1\"># Everything as before.</span>\n    <span class=\"o\">...</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">clean</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"n\">cleaned_data</span> <span class=\"o\">=</span> <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span><span class=\"n\">clean</span><span class=\"p\">()</span>\n        <span class=\"n\">cc_myself</span> <span class=\"o\">=</span> <span class=\"n\">cleaned_data</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s2\">&quot;cc_myself&quot;</span><span class=\"p\">)</span>\n        <span class=\"n\">subject</span> <span class=\"o\">=</span> <span class=\"n\">cleaned_data</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">)</span>\n\n        <span class=\"k\">if</span> <span class=\"n\">cc_myself</span> <span class=\"ow\">and</span> <span class=\"n\">subject</span> <span class=\"ow\">and</span> <span class=\"s2\">&quot;help&quot;</span> <span class=\"ow\">not</span> <span class=\"ow\">in</span> <span class=\"n\">subject</span><span class=\"p\">:</span>\n            <span class=\"n\">msg</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;Must put &#39;help&#39; in subject when cc&#39;ing yourself.&quot;</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">add_error</span><span class=\"p\">(</span><span class=\"s1\">&#39;cc_myself&#39;</span><span class=\"p\">,</span> <span class=\"n\">msg</span><span class=\"p\">)</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">add_error</span><span class=\"p\">(</span><span class=\"s1\">&#39;subject&#39;</span><span class=\"p\">,</span> <span class=\"n\">msg</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>The second argument of <code class=\"docutils literal notranslate\"><span class=\"pre\">add_error()</span></code> can be a simple string, or preferably\nan instance of <code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code>. See <a class=\"reference internal\" href=\"#raising-validation-error\"><span class=\"std std-ref\">Raising ValidationError</span></a> for\nmore details. Note that <code class=\"docutils literal notranslate\"><span class=\"pre\">add_error()</span></code> automatically removes the field\nfrom <code class=\"docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code>.</p>\n</section>\n</section>","rootId":"form-and-field-validation","toc":[{"title":"Raising ValidationError","anchor":"raising-validationerror","children":[{"title":"Raising multiple errors","anchor":"raising-multiple-errors","children":[]}]},{"title":"Using validation in practice","anchor":"using-validation-in-practice","children":[{"title":"Using validators","anchor":"using-validators","children":[]},{"title":"Form field default cleaning","anchor":"form-field-default-cleaning","children":[]},{"title":"Cleaning a specific field attribute","anchor":"cleaning-a-specific-field-attribute","children":[]},{"title":"Cleaning and validating fields that depend on each other","anchor":"cleaning-and-validating-fields-that-depend-on-each-other","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/zh-hans/2.1/ref/"},{"docname":"ref/forms/index","title":"Forms","url":"/zh-hans/2.1/ref/forms/"}],"prev":{"docname":"ref/forms/widgets","title":"Widgets","url":"/zh-hans/2.1/ref/forms/widgets/"},"next":{"docname":"ref/middleware","title":"Middleware","url":"/zh-hans/2.1/ref/middleware/"},"formats":{"html":"/zh-hans/2.1/ref/forms/validation/","markdown":"/zh-hans/2.1/ref/forms/validation.md","json":"/zh-hans/2.1/ref/forms/validation.json"},"source":"https://github.com/django/django/blob/stable/2.1.x/docs/ref/forms/validation.txt","official":"https://docs.djangoproject.com/zh-hans/2.1/ref/forms/validation/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}