Sophie

Sophie

distrib > Mageia > 7 > x86_64 > by-pkgid > 481c2de1450e70fa8fdc1e3abf72606b > files > 888

python-django-doc-1.11.20-1.mga7.noarch.rpm


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Validators &#8212; Django 1.11.20 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Built-in Views" href="views.html" />
    <link rel="prev" title="Django Utils" href="utils.html" />



 
<script type="text/javascript" src="../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 1.11.20 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../index.html">Home</a>  |
        <a title="Table of contents" href="../contents.html">Table of contents</a>  |
        <a title="Global index" href="../genindex.html">Index</a>  |
        <a title="Module index" href="../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="utils.html" title="Django Utils">previous</a>
     |
    <a href="index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="views.html" title="Built-in Views">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-validators">
            
  <div class="section" id="s-module-django.core.validators">
<span id="s-validators"></span><span id="module-django.core.validators"></span><span id="validators"></span><h1>Validators<a class="headerlink" href="#module-django.core.validators" title="Permalink to this headline">¶</a></h1>
<div class="section" id="s-writing-validators">
<span id="writing-validators"></span><h2>Writing validators<a class="headerlink" href="#writing-validators" title="Permalink to this headline">¶</a></h2>
<p>A validator is a callable that takes a value and raises a
<a class="reference internal" href="exceptions.html#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 it doesn’t meet some
criteria. Validators can be useful for re-using validation logic between
different types of fields.</p>
<p>For example, here’s a validator that only allows even numbers:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="k">import</span> <span class="n">ValidationError</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ugettext_lazy</span> <span class="k">as</span> <span class="n">_</span>

<span class="k">def</span> <span class="nf">validate_even</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">value</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span>
            <span class="n">_</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%(value)s</span><span class="s1"> is not an even number&#39;</span><span class="p">),</span>
            <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="n">value</span><span class="p">},</span>
        <span class="p">)</span>
</pre></div>
</div>
<p>You can add this to a model field via the field’s <a class="reference internal" href="models/fields.html#django.db.models.Field.validators" title="django.db.models.Field.validators"><code class="xref py py-attr docutils literal notranslate"><span class="pre">validators</span></code></a>
argument:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">MyModel</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">even_field</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">IntegerField</span><span class="p">(</span><span class="n">validators</span><span class="o">=</span><span class="p">[</span><span class="n">validate_even</span><span class="p">])</span>
</pre></div>
</div>
<p>Because values are converted to Python before validators are run, you can even
use the same validator with forms:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django</span> <span class="k">import</span> <span class="n">forms</span>

<span class="k">class</span> <span class="nc">MyForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
    <span class="n">even_field</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">IntegerField</span><span class="p">(</span><span class="n">validators</span><span class="o">=</span><span class="p">[</span><span class="n">validate_even</span><span class="p">])</span>
</pre></div>
</div>
<p>You can also use a class with a <code class="docutils literal notranslate"><span class="pre">__call__()</span></code> method for more complex or
configurable validators. <a class="reference internal" href="#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>, for example, uses this
technique. If a class-based validator is used in the
<a class="reference internal" href="models/fields.html#django.db.models.Field.validators" title="django.db.models.Field.validators"><code class="xref py py-attr docutils literal notranslate"><span class="pre">validators</span></code></a> model field option, you should make
sure it is <a class="reference internal" href="../topics/migrations.html#migration-serializing"><span class="std std-ref">serializable by the migration framework</span></a> by adding <a class="reference internal" href="../topics/migrations.html#custom-deconstruct-method"><span class="std std-ref">deconstruct()</span></a> and <code class="docutils literal notranslate"><span class="pre">__eq__()</span></code> methods.</p>
</div>
<div class="section" id="s-how-validators-are-run">
<span id="how-validators-are-run"></span><h2>How validators are run<a class="headerlink" href="#how-validators-are-run" title="Permalink to this headline">¶</a></h2>
<p>See the <a class="reference internal" href="forms/validation.html"><span class="doc">form validation</span></a> for more information on
how validators are run in forms, and <a class="reference internal" href="models/instances.html#validating-objects"><span class="std std-ref">Validating objects</span></a> for how they’re run in models. Note that validators will
not be run automatically when you save a model, but if you are using a
<a class="reference internal" href="../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelForm</span></code></a>, it will run your validators on any fields
that are included in your form. See the
<a class="reference internal" href="../topics/forms/modelforms.html"><span class="doc">ModelForm documentation</span></a> for information on
how model validation interacts with forms.</p>
</div>
<div class="section" id="s-built-in-validators">
<span id="built-in-validators"></span><h2>Built-in validators<a class="headerlink" href="#built-in-validators" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="#module-django.core.validators" title="django.core.validators: Validation utilities and base classes"><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.core.validators</span></code></a> module contains a collection of callable
validators for use with model and form fields. They’re used internally but
are available for use with your own fields, too. They can be used in addition
to, or in lieu of custom <code class="docutils literal notranslate"><span class="pre">field.clean()</span></code> methods.</p>
<div class="section" id="s-regexvalidator">
<span id="regexvalidator"></span><h3><code class="docutils literal notranslate"><span class="pre">RegexValidator</span></code><a class="headerlink" href="#regexvalidator" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.core.validators.RegexValidator">
<em class="property">class </em><code class="descname">RegexValidator</code>(<em>regex=None</em>, <em>message=None</em>, <em>code=None</em>, <em>inverse_match=None</em>, <em>flags=0</em>)<a class="reference internal" href="../_modules/django/core/validators.html#RegexValidator"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.core.validators.RegexValidator" title="Permalink to this definition">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>regex</strong> – If not <code class="docutils literal notranslate"><span class="pre">None</span></code>, overrides <a class="reference internal" href="#django.core.validators.RegexValidator.regex" title="django.core.validators.RegexValidator.regex"><code class="xref py py-attr docutils literal notranslate"><span class="pre">regex</span></code></a>. Can be a regular
expression string or a pre-compiled regular expression.</li>
<li><strong>message</strong> – If not <code class="docutils literal notranslate"><span class="pre">None</span></code>, overrides <a class="reference internal" href="#django.core.validators.RegexValidator.message" title="django.core.validators.RegexValidator.message"><code class="xref py py-attr docutils literal notranslate"><span class="pre">message</span></code></a>.</li>
<li><strong>code</strong> – If not <code class="docutils literal notranslate"><span class="pre">None</span></code>, overrides <a class="reference internal" href="#django.core.validators.RegexValidator.code" title="django.core.validators.RegexValidator.code"><code class="xref py py-attr docutils literal notranslate"><span class="pre">code</span></code></a>.</li>
<li><strong>inverse_match</strong> – If not <code class="docutils literal notranslate"><span class="pre">None</span></code>, overrides <a class="reference internal" href="#django.core.validators.RegexValidator.inverse_match" title="django.core.validators.RegexValidator.inverse_match"><code class="xref py py-attr docutils literal notranslate"><span class="pre">inverse_match</span></code></a>.</li>
<li><strong>flags</strong> – If not <code class="docutils literal notranslate"><span class="pre">None</span></code>, overrides <a class="reference internal" href="#django.core.validators.RegexValidator.flags" title="django.core.validators.RegexValidator.flags"><code class="xref py py-attr docutils literal notranslate"><span class="pre">flags</span></code></a>. In that case,
<a class="reference internal" href="#django.core.validators.RegexValidator.regex" title="django.core.validators.RegexValidator.regex"><code class="xref py py-attr docutils literal notranslate"><span class="pre">regex</span></code></a> must be a regular expression string, or
<code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code> is raised.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<dl class="attribute">
<dt id="django.core.validators.RegexValidator.regex">
<code class="descname">regex</code><a class="headerlink" href="#django.core.validators.RegexValidator.regex" title="Permalink to this definition">¶</a></dt>
<dd><p>The regular expression pattern to search for within the provided
<code class="docutils literal notranslate"><span class="pre">value</span></code>, or a pre-compiled regular expression. By default, raises a
<a class="reference internal" href="exceptions.html#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> with <a class="reference internal" href="#django.core.validators.RegexValidator.message" title="django.core.validators.RegexValidator.message"><code class="xref py py-attr docutils literal notranslate"><span class="pre">message</span></code></a>
and <a class="reference internal" href="#django.core.validators.RegexValidator.code" title="django.core.validators.RegexValidator.code"><code class="xref py py-attr docutils literal notranslate"><span class="pre">code</span></code></a> if a match is not found. That standard behavior can
be reversed by setting <a class="reference internal" href="#django.core.validators.RegexValidator.inverse_match" title="django.core.validators.RegexValidator.inverse_match"><code class="xref py py-attr docutils literal notranslate"><span class="pre">inverse_match</span></code></a> to <code class="docutils literal notranslate"><span class="pre">True</span></code>, in which case
the <a class="reference internal" href="exceptions.html#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> is raised when a
match <strong>is</strong> found. By default, matches any string (including an empty
string).</p>
</dd></dl>

<dl class="attribute">
<dt id="django.core.validators.RegexValidator.message">
<code class="descname">message</code><a class="headerlink" href="#django.core.validators.RegexValidator.message" title="Permalink to this definition">¶</a></dt>
<dd><p>The error message used by
<a class="reference internal" href="exceptions.html#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 validation fails.
Defaults to <code class="docutils literal notranslate"><span class="pre">&quot;Enter</span> <span class="pre">a</span> <span class="pre">valid</span> <span class="pre">value&quot;</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.core.validators.RegexValidator.code">
<code class="descname">code</code><a class="headerlink" href="#django.core.validators.RegexValidator.code" title="Permalink to this definition">¶</a></dt>
<dd><p>The error code used by <a class="reference internal" href="exceptions.html#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 validation fails. Defaults to <code class="docutils literal notranslate"><span class="pre">&quot;invalid&quot;</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.core.validators.RegexValidator.inverse_match">
<code class="descname">inverse_match</code><a class="headerlink" href="#django.core.validators.RegexValidator.inverse_match" title="Permalink to this definition">¶</a></dt>
<dd><p>The match mode for <a class="reference internal" href="#django.core.validators.RegexValidator.regex" title="django.core.validators.RegexValidator.regex"><code class="xref py py-attr docutils literal notranslate"><span class="pre">regex</span></code></a>. Defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.core.validators.RegexValidator.flags">
<code class="descname">flags</code><a class="headerlink" href="#django.core.validators.RegexValidator.flags" title="Permalink to this definition">¶</a></dt>
<dd><p>The flags used when compiling the regular expression string
<a class="reference internal" href="#django.core.validators.RegexValidator.regex" title="django.core.validators.RegexValidator.regex"><code class="xref py py-attr docutils literal notranslate"><span class="pre">regex</span></code></a>. If <a class="reference internal" href="#django.core.validators.RegexValidator.regex" title="django.core.validators.RegexValidator.regex"><code class="xref py py-attr docutils literal notranslate"><span class="pre">regex</span></code></a> is a pre-compiled regular expression,
and <a class="reference internal" href="#django.core.validators.RegexValidator.flags" title="django.core.validators.RegexValidator.flags"><code class="xref py py-attr docutils literal notranslate"><span class="pre">flags</span></code></a> is overridden, <code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code> is raised. Defaults
to <code class="docutils literal notranslate"><span class="pre">0</span></code>.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-emailvalidator">
<span id="emailvalidator"></span><h3><code class="docutils literal notranslate"><span class="pre">EmailValidator</span></code><a class="headerlink" href="#emailvalidator" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.core.validators.EmailValidator">
<em class="property">class </em><code class="descname">EmailValidator</code>(<em>message=None</em>, <em>code=None</em>, <em>whitelist=None</em>)<a class="reference internal" href="../_modules/django/core/validators.html#EmailValidator"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.core.validators.EmailValidator" title="Permalink to this definition">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>message</strong> – If not <code class="docutils literal notranslate"><span class="pre">None</span></code>, overrides <a class="reference internal" href="#django.core.validators.EmailValidator.message" title="django.core.validators.EmailValidator.message"><code class="xref py py-attr docutils literal notranslate"><span class="pre">message</span></code></a>.</li>
<li><strong>code</strong> – If not <code class="docutils literal notranslate"><span class="pre">None</span></code>, overrides <a class="reference internal" href="#django.core.validators.EmailValidator.code" title="django.core.validators.EmailValidator.code"><code class="xref py py-attr docutils literal notranslate"><span class="pre">code</span></code></a>.</li>
<li><strong>whitelist</strong> – If not <code class="docutils literal notranslate"><span class="pre">None</span></code>, overrides <a class="reference internal" href="#django.core.validators.EmailValidator.whitelist" title="django.core.validators.EmailValidator.whitelist"><code class="xref py py-attr docutils literal notranslate"><span class="pre">whitelist</span></code></a>.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<dl class="attribute">
<dt id="django.core.validators.EmailValidator.message">
<code class="descname">message</code><a class="headerlink" href="#django.core.validators.EmailValidator.message" title="Permalink to this definition">¶</a></dt>
<dd><p>The error message used by
<a class="reference internal" href="exceptions.html#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 validation fails.
Defaults to <code class="docutils literal notranslate"><span class="pre">&quot;Enter</span> <span class="pre">a</span> <span class="pre">valid</span> <span class="pre">email</span> <span class="pre">address&quot;</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.core.validators.EmailValidator.code">
<code class="descname">code</code><a class="headerlink" href="#django.core.validators.EmailValidator.code" title="Permalink to this definition">¶</a></dt>
<dd><p>The error code used by <a class="reference internal" href="exceptions.html#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 validation fails. Defaults to <code class="docutils literal notranslate"><span class="pre">&quot;invalid&quot;</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.core.validators.EmailValidator.whitelist">
<code class="descname">whitelist</code><a class="headerlink" href="#django.core.validators.EmailValidator.whitelist" title="Permalink to this definition">¶</a></dt>
<dd><p>Whitelist of email domains to allow. By default, a regular expression
(the <code class="docutils literal notranslate"><span class="pre">domain_regex</span></code> attribute) is used to validate whatever appears
after the &#64; sign. However, if that string appears in the whitelist, this
validation is bypassed. If not provided, the default whitelist is
<code class="docutils literal notranslate"><span class="pre">['localhost']</span></code>. Other domains that don’t contain a dot won’t pass
validation, so you’d need to whitelist them as necessary.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-urlvalidator">
<span id="urlvalidator"></span><h3><code class="docutils literal notranslate"><span class="pre">URLValidator</span></code><a class="headerlink" href="#urlvalidator" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.core.validators.URLValidator">
<em class="property">class </em><code class="descname">URLValidator</code>(<em>schemes=None</em>, <em>regex=None</em>, <em>message=None</em>, <em>code=None</em>)<a class="reference internal" href="../_modules/django/core/validators.html#URLValidator"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.core.validators.URLValidator" title="Permalink to this definition">¶</a></dt>
<dd><p>A <a class="reference internal" href="#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> that ensures a value looks like a URL, and raises
an error code of <code class="docutils literal notranslate"><span class="pre">'invalid'</span></code> if it doesn’t.</p>
<p>Loopback addresses and reserved IP spaces are considered valid. Literal
IPv6 addresses (<span class="target" id="index-0"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2732.html"><strong>RFC 2732</strong></a>) and unicode domains are both supported.</p>
<p>In addition to the optional arguments of its parent <a class="reference internal" href="#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>
class, <code class="docutils literal notranslate"><span class="pre">URLValidator</span></code> accepts an extra optional attribute:</p>
<dl class="attribute">
<dt id="django.core.validators.URLValidator.schemes">
<code class="descname">schemes</code><a class="headerlink" href="#django.core.validators.URLValidator.schemes" title="Permalink to this definition">¶</a></dt>
<dd><p>URL/URI scheme list to validate against. If not provided, the default
list is <code class="docutils literal notranslate"><span class="pre">['http',</span> <span class="pre">'https',</span> <span class="pre">'ftp',</span> <span class="pre">'ftps']</span></code>. As a reference, the IANA
website provides a full list of <a class="reference external" href="https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml">valid URI schemes</a>.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-validate-email">
<span id="validate-email"></span><h3><code class="docutils literal notranslate"><span class="pre">validate_email</span></code><a class="headerlink" href="#validate-email" title="Permalink to this headline">¶</a></h3>
<dl class="data">
<dt id="django.core.validators.validate_email">
<code class="descname">validate_email</code><a class="headerlink" href="#django.core.validators.validate_email" title="Permalink to this definition">¶</a></dt>
<dd><p>An <a class="reference internal" href="#django.core.validators.EmailValidator" title="django.core.validators.EmailValidator"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailValidator</span></code></a> instance without any customizations.</p>
</dd></dl>

</div>
<div class="section" id="s-validate-slug">
<span id="validate-slug"></span><h3><code class="docutils literal notranslate"><span class="pre">validate_slug</span></code><a class="headerlink" href="#validate-slug" title="Permalink to this headline">¶</a></h3>
<dl class="data">
<dt id="django.core.validators.validate_slug">
<code class="descname">validate_slug</code><a class="headerlink" href="#django.core.validators.validate_slug" title="Permalink to this definition">¶</a></dt>
<dd><p>A <a class="reference internal" href="#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> instance that ensures a value consists of only
letters, numbers, underscores or hyphens.</p>
</dd></dl>

</div>
<div class="section" id="s-validate-unicode-slug">
<span id="validate-unicode-slug"></span><h3><code class="docutils literal notranslate"><span class="pre">validate_unicode_slug</span></code><a class="headerlink" href="#validate-unicode-slug" title="Permalink to this headline">¶</a></h3>
<dl class="data">
<dt id="django.core.validators.validate_unicode_slug">
<code class="descname">validate_unicode_slug</code><a class="headerlink" href="#django.core.validators.validate_unicode_slug" title="Permalink to this definition">¶</a></dt>
<dd><p>A <a class="reference internal" href="#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> instance that ensures a value consists of only
Unicode letters, numbers, underscores, or hyphens.</p>
</dd></dl>

</div>
<div class="section" id="s-validate-ipv4-address">
<span id="validate-ipv4-address"></span><h3><code class="docutils literal notranslate"><span class="pre">validate_ipv4_address</span></code><a class="headerlink" href="#validate-ipv4-address" title="Permalink to this headline">¶</a></h3>
<dl class="data">
<dt id="django.core.validators.validate_ipv4_address">
<code class="descname">validate_ipv4_address</code><a class="headerlink" href="#django.core.validators.validate_ipv4_address" title="Permalink to this definition">¶</a></dt>
<dd><p>A <a class="reference internal" href="#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> instance that ensures a value looks like an IPv4
address.</p>
</dd></dl>

</div>
<div class="section" id="s-validate-ipv6-address">
<span id="validate-ipv6-address"></span><h3><code class="docutils literal notranslate"><span class="pre">validate_ipv6_address</span></code><a class="headerlink" href="#validate-ipv6-address" title="Permalink to this headline">¶</a></h3>
<dl class="data">
<dt id="django.core.validators.validate_ipv6_address">
<code class="descname">validate_ipv6_address</code><a class="reference internal" href="../_modules/django/core/validators.html#validate_ipv6_address"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.core.validators.validate_ipv6_address" title="Permalink to this definition">¶</a></dt>
<dd><p>Uses <code class="docutils literal notranslate"><span class="pre">django.utils.ipv6</span></code> to check the validity of an IPv6 address.</p>
</dd></dl>

</div>
<div class="section" id="s-validate-ipv46-address">
<span id="validate-ipv46-address"></span><h3><code class="docutils literal notranslate"><span class="pre">validate_ipv46_address</span></code><a class="headerlink" href="#validate-ipv46-address" title="Permalink to this headline">¶</a></h3>
<dl class="data">
<dt id="django.core.validators.validate_ipv46_address">
<code class="descname">validate_ipv46_address</code><a class="reference internal" href="../_modules/django/core/validators.html#validate_ipv46_address"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.core.validators.validate_ipv46_address" title="Permalink to this definition">¶</a></dt>
<dd><p>Uses both <code class="docutils literal notranslate"><span class="pre">validate_ipv4_address</span></code> and <code class="docutils literal notranslate"><span class="pre">validate_ipv6_address</span></code> to
ensure a value is either a valid IPv4 or IPv6 address.</p>
</dd></dl>

</div>
<div class="section" id="s-validate-comma-separated-integer-list">
<span id="validate-comma-separated-integer-list"></span><h3><code class="docutils literal notranslate"><span class="pre">validate_comma_separated_integer_list</span></code><a class="headerlink" href="#validate-comma-separated-integer-list" title="Permalink to this headline">¶</a></h3>
<dl class="data">
<dt id="django.core.validators.validate_comma_separated_integer_list">
<code class="descname">validate_comma_separated_integer_list</code><a class="headerlink" href="#django.core.validators.validate_comma_separated_integer_list" title="Permalink to this definition">¶</a></dt>
<dd><p>A <a class="reference internal" href="#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> instance that ensures a value is a
comma-separated list of integers.</p>
</dd></dl>

</div>
<div class="section" id="s-int-list-validator">
<span id="int-list-validator"></span><h3><code class="docutils literal notranslate"><span class="pre">int_list_validator</span></code><a class="headerlink" href="#int-list-validator" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="django.core.validators.int_list_validator">
<code class="descname">int_list_validator</code>(<em>sep='</em>, <em>'</em>, <em>message=None</em>, <em>code='invalid'</em>, <em>allow_negative=False</em>)<a class="reference internal" href="../_modules/django/core/validators.html#int_list_validator"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.core.validators.int_list_validator" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a <a class="reference internal" href="#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> instance that ensures a string consists
of integers separated by <code class="docutils literal notranslate"><span class="pre">sep</span></code>. It allows negative integers when
<code class="docutils literal notranslate"><span class="pre">allow_negative</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.10:</span> <p>The <code class="docutils literal notranslate"><span class="pre">allow_negative</span></code> parameter was added.</p>
</div>
</dd></dl>

</div>
<div class="section" id="s-maxvaluevalidator">
<span id="maxvaluevalidator"></span><h3><code class="docutils literal notranslate"><span class="pre">MaxValueValidator</span></code><a class="headerlink" href="#maxvaluevalidator" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.core.validators.MaxValueValidator">
<em class="property">class </em><code class="descname">MaxValueValidator</code>(<em>max_value</em>, <em>message=None</em>)<a class="reference internal" href="../_modules/django/core/validators.html#MaxValueValidator"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.core.validators.MaxValueValidator" title="Permalink to this definition">¶</a></dt>
<dd><p>Raises a <a class="reference internal" href="exceptions.html#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> with a code of
<code class="docutils literal notranslate"><span class="pre">'max_value'</span></code> if <code class="docutils literal notranslate"><span class="pre">value</span></code> is greater than <code class="docutils literal notranslate"><span class="pre">max_value</span></code>.</p>
</dd></dl>

</div>
<div class="section" id="s-minvaluevalidator">
<span id="minvaluevalidator"></span><h3><code class="docutils literal notranslate"><span class="pre">MinValueValidator</span></code><a class="headerlink" href="#minvaluevalidator" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.core.validators.MinValueValidator">
<em class="property">class </em><code class="descname">MinValueValidator</code>(<em>min_value</em>, <em>message=None</em>)<a class="reference internal" href="../_modules/django/core/validators.html#MinValueValidator"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.core.validators.MinValueValidator" title="Permalink to this definition">¶</a></dt>
<dd><p>Raises a <a class="reference internal" href="exceptions.html#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> with a code of
<code class="docutils literal notranslate"><span class="pre">'min_value'</span></code> if <code class="docutils literal notranslate"><span class="pre">value</span></code> is less than <code class="docutils literal notranslate"><span class="pre">min_value</span></code>.</p>
</dd></dl>

</div>
<div class="section" id="s-maxlengthvalidator">
<span id="maxlengthvalidator"></span><h3><code class="docutils literal notranslate"><span class="pre">MaxLengthValidator</span></code><a class="headerlink" href="#maxlengthvalidator" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.core.validators.MaxLengthValidator">
<em class="property">class </em><code class="descname">MaxLengthValidator</code>(<em>max_length</em>, <em>message=None</em>)<a class="reference internal" href="../_modules/django/core/validators.html#MaxLengthValidator"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.core.validators.MaxLengthValidator" title="Permalink to this definition">¶</a></dt>
<dd><p>Raises a <a class="reference internal" href="exceptions.html#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> with a code of
<code class="docutils literal notranslate"><span class="pre">'max_length'</span></code> if the length of <code class="docutils literal notranslate"><span class="pre">value</span></code> is greater than <code class="docutils literal notranslate"><span class="pre">max_length</span></code>.</p>
</dd></dl>

</div>
<div class="section" id="s-minlengthvalidator">
<span id="minlengthvalidator"></span><h3><code class="docutils literal notranslate"><span class="pre">MinLengthValidator</span></code><a class="headerlink" href="#minlengthvalidator" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.core.validators.MinLengthValidator">
<em class="property">class </em><code class="descname">MinLengthValidator</code>(<em>min_length</em>, <em>message=None</em>)<a class="reference internal" href="../_modules/django/core/validators.html#MinLengthValidator"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.core.validators.MinLengthValidator" title="Permalink to this definition">¶</a></dt>
<dd><p>Raises a <a class="reference internal" href="exceptions.html#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> with a code of
<code class="docutils literal notranslate"><span class="pre">'min_length'</span></code> if the length of <code class="docutils literal notranslate"><span class="pre">value</span></code> is less than <code class="docutils literal notranslate"><span class="pre">min_length</span></code>.</p>
</dd></dl>

</div>
<div class="section" id="s-decimalvalidator">
<span id="decimalvalidator"></span><h3><code class="docutils literal notranslate"><span class="pre">DecimalValidator</span></code><a class="headerlink" href="#decimalvalidator" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.core.validators.DecimalValidator">
<em class="property">class </em><code class="descname">DecimalValidator</code>(<em>max_digits</em>, <em>decimal_places</em>)<a class="reference internal" href="../_modules/django/core/validators.html#DecimalValidator"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.core.validators.DecimalValidator" title="Permalink to this definition">¶</a></dt>
<dd><p>Raises <a class="reference internal" href="exceptions.html#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> with the following
codes:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">'max_digits'</span></code> if the number of digits is larger than <code class="docutils literal notranslate"><span class="pre">max_digits</span></code>.</li>
<li><code class="docutils literal notranslate"><span class="pre">'max_decimal_places'</span></code> if the number of decimals is larger than
<code class="docutils literal notranslate"><span class="pre">decimal_places</span></code>.</li>
<li><code class="docutils literal notranslate"><span class="pre">'max_whole_digits'</span></code> if the number of whole digits is larger than
the difference between <code class="docutils literal notranslate"><span class="pre">max_digits</span></code> and <code class="docutils literal notranslate"><span class="pre">decimal_places</span></code>.</li>
</ul>
</dd></dl>

</div>
<div class="section" id="s-fileextensionvalidator">
<span id="fileextensionvalidator"></span><h3><code class="docutils literal notranslate"><span class="pre">FileExtensionValidator</span></code><a class="headerlink" href="#fileextensionvalidator" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.core.validators.FileExtensionValidator">
<em class="property">class </em><code class="descname">FileExtensionValidator</code>(<em>allowed_extensions</em>, <em>message</em>, <em>code</em>)<a class="reference internal" href="../_modules/django/core/validators.html#FileExtensionValidator"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.core.validators.FileExtensionValidator" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.11.</span> </div>
<p>Raises a <a class="reference internal" href="exceptions.html#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> with a code of
<code class="docutils literal notranslate"><span class="pre">'invalid_extension'</span></code> if the extension of <code class="docutils literal notranslate"><span class="pre">value.name</span></code> (<code class="docutils literal notranslate"><span class="pre">value</span></code> is
a <a class="reference internal" href="files/file.html#django.core.files.File" title="django.core.files.File"><code class="xref py py-class docutils literal notranslate"><span class="pre">File</span></code></a>) isn’t found in <code class="docutils literal notranslate"><span class="pre">allowed_extensions</span></code>.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Don’t rely on validation of the file extension to determine a file’s
type. Files can be renamed to have any extension no matter what data
they contain.</p>
</div>
</dd></dl>

</div>
<div class="section" id="s-validate-image-file-extension">
<span id="validate-image-file-extension"></span><h3><code class="docutils literal notranslate"><span class="pre">validate_image_file_extension</span></code><a class="headerlink" href="#validate-image-file-extension" title="Permalink to this headline">¶</a></h3>
<dl class="data">
<dt id="django.core.validators.validate_image_file_extension">
<code class="descname">validate_image_file_extension</code><a class="headerlink" href="#django.core.validators.validate_image_file_extension" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.11.</span> </div>
<p>Uses Pillow to ensure that <code class="docutils literal notranslate"><span class="pre">value.name</span></code> (<code class="docutils literal notranslate"><span class="pre">value</span></code> is a
<a class="reference internal" href="files/file.html#django.core.files.File" title="django.core.files.File"><code class="xref py py-class docutils literal notranslate"><span class="pre">File</span></code></a>) has <a class="reference external" href="https://pillow.readthedocs.io/en/latest/handbook/image-file-formats.html">a valid image extension</a>.</p>
</dd></dl>

</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Validators</a><ul>
<li><a class="reference internal" href="#writing-validators">Writing validators</a></li>
<li><a class="reference internal" href="#how-validators-are-run">How validators are run</a></li>
<li><a class="reference internal" href="#built-in-validators">Built-in validators</a><ul>
<li><a class="reference internal" href="#regexvalidator"><code class="docutils literal notranslate"><span class="pre">RegexValidator</span></code></a></li>
<li><a class="reference internal" href="#emailvalidator"><code class="docutils literal notranslate"><span class="pre">EmailValidator</span></code></a></li>
<li><a class="reference internal" href="#urlvalidator"><code class="docutils literal notranslate"><span class="pre">URLValidator</span></code></a></li>
<li><a class="reference internal" href="#validate-email"><code class="docutils literal notranslate"><span class="pre">validate_email</span></code></a></li>
<li><a class="reference internal" href="#validate-slug"><code class="docutils literal notranslate"><span class="pre">validate_slug</span></code></a></li>
<li><a class="reference internal" href="#validate-unicode-slug"><code class="docutils literal notranslate"><span class="pre">validate_unicode_slug</span></code></a></li>
<li><a class="reference internal" href="#validate-ipv4-address"><code class="docutils literal notranslate"><span class="pre">validate_ipv4_address</span></code></a></li>
<li><a class="reference internal" href="#validate-ipv6-address"><code class="docutils literal notranslate"><span class="pre">validate_ipv6_address</span></code></a></li>
<li><a class="reference internal" href="#validate-ipv46-address"><code class="docutils literal notranslate"><span class="pre">validate_ipv46_address</span></code></a></li>
<li><a class="reference internal" href="#validate-comma-separated-integer-list"><code class="docutils literal notranslate"><span class="pre">validate_comma_separated_integer_list</span></code></a></li>
<li><a class="reference internal" href="#int-list-validator"><code class="docutils literal notranslate"><span class="pre">int_list_validator</span></code></a></li>
<li><a class="reference internal" href="#maxvaluevalidator"><code class="docutils literal notranslate"><span class="pre">MaxValueValidator</span></code></a></li>
<li><a class="reference internal" href="#minvaluevalidator"><code class="docutils literal notranslate"><span class="pre">MinValueValidator</span></code></a></li>
<li><a class="reference internal" href="#maxlengthvalidator"><code class="docutils literal notranslate"><span class="pre">MaxLengthValidator</span></code></a></li>
<li><a class="reference internal" href="#minlengthvalidator"><code class="docutils literal notranslate"><span class="pre">MinLengthValidator</span></code></a></li>
<li><a class="reference internal" href="#decimalvalidator"><code class="docutils literal notranslate"><span class="pre">DecimalValidator</span></code></a></li>
<li><a class="reference internal" href="#fileextensionvalidator"><code class="docutils literal notranslate"><span class="pre">FileExtensionValidator</span></code></a></li>
<li><a class="reference internal" href="#validate-image-file-extension"><code class="docutils literal notranslate"><span class="pre">validate_image_file_extension</span></code></a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="utils.html"
                        title="previous chapter">Django Utils</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="views.html"
                        title="next chapter">Built-in Views</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/ref/validators.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Feb 11, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="utils.html" title="Django Utils">previous</a>
     |
    <a href="index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="views.html" title="Built-in Views">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>