{"title":"The Forms API","version":"5.1","locale":"pl","docname":"ref/forms/api","url":"/pl/5.1/ref/forms/api/","canonical":"https://djangodocs.dev/pl/5.1/ref/forms/api/","summary":"About this document This document covers the gritty details of Django’s forms API. You should read the introduction to working with forms first. Bound and unbound…","html":"<span id=\"the-forms-api\"></span><h1>The Forms API<a class=\"heading-anchor\" href=\"#module-django.forms\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<aside class=\"admonition-about-this-document admonition\">\n<p class=\"admonition-title\">About this document</p>\n<p>This document covers the gritty details of Django’s forms API. You should\nread the <a class=\"reference internal\" href=\"/pl/5.1/topics/forms/\"><span class=\"doc\">introduction to working with forms</span></a>\nfirst.</p>\n</aside>\n<section id=\"bound-and-unbound-forms\">\n<span id=\"ref-forms-api-bound-unbound\"></span><h2>Bound and unbound forms<a class=\"heading-anchor\" href=\"#bound-and-unbound-forms\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>A <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> instance is either <strong>bound</strong> to a set of data, or <strong>unbound</strong>.</p>\n<ul class=\"simple\">\n<li><p>If it’s <strong>bound</strong> to a set of data, it’s capable of validating that data\nand rendering the form as HTML with the data displayed in the HTML.</p></li>\n<li><p>If it’s <strong>unbound</strong>, it cannot do validation (because there’s no data to\nvalidate!), but it can still render the blank form as HTML.</p></li>\n</ul>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">Form</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>To create an unbound <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> instance, instantiate the class:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">()</span>\n</code></pre></div>\n<p>To bind data to a form, pass the data as a dictionary as the first parameter to\nyour <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> class constructor:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">data</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;hello&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;message&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Hi there&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;sender&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;foo@example.com&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;cc_myself&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span><span class=\"p\">}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>In this dictionary, the keys are the field names, which correspond to the\nattributes in your <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> class. The values are the data you’re trying to\nvalidate. These will usually be strings, but there’s no requirement that they be\nstrings; the type of data you pass depends on the <a class=\"reference internal\" href=\"/pl/5.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>, as we’ll see\nin a moment.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.is_bound\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">is_bound</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.is_bound\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>If you need to distinguish between bound and unbound form instances at runtime,\ncheck the value of the form’s <a class=\"reference internal\" href=\"#django.forms.Form.is_bound\" title=\"django.forms.Form.is_bound\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">is_bound</span></code></a> attribute:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">is_bound</span>\n<span class=\"go\">False</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">({</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;hello&quot;</span><span class=\"p\">})</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">is_bound</span>\n<span class=\"go\">True</span>\n</code></pre></div>\n<p>Note that passing an empty dictionary creates a <em>bound</em> form with empty data:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">({})</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">is_bound</span>\n<span class=\"go\">True</span>\n</code></pre></div>\n<p>If you have a bound <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> instance and want to change the data somehow,\nor if you want to bind an unbound <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> instance to some data, create\nanother <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> instance. There is no way to change data in a\n<a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> instance. Once a <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> instance has been created, you\nshould consider its data immutable, whether it has data or not.</p>\n</section>\n<section id=\"using-forms-to-validate-data\">\n<h2>Using forms to validate data<a class=\"heading-anchor\" href=\"#using-forms-to-validate-data\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.clean\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">clean</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Form.clean\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Implement a <code class=\"docutils literal notranslate\"><span class=\"pre\">clean()</span></code> method on your <code class=\"docutils literal notranslate\"><span class=\"pre\">Form</span></code> when you must add custom\nvalidation for fields that are interdependent. See\n<a class=\"reference internal\" href=\"/pl/5.1/ref/forms/validation/#validating-fields-with-clean\"><span class=\"std std-ref\">Cleaning and validating fields that depend on each other</span></a> for example usage.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.is_valid\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">is_valid</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Form.is_valid\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The primary task of a <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> object is to validate data. With a bound\n<a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> instance, call the <a class=\"reference internal\" href=\"#django.forms.Form.is_valid\" title=\"django.forms.Form.is_valid\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">is_valid()</span></code></a> method to run validation\nand return a boolean designating whether the data was valid:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">data</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;hello&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;message&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Hi there&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;sender&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;foo@example.com&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;cc_myself&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span><span class=\"p\">}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">is_valid</span><span class=\"p\">()</span>\n<span class=\"go\">True</span>\n</code></pre></div>\n<p>Let’s try with some invalid data. In this case, <code class=\"docutils literal notranslate\"><span class=\"pre\">subject</span></code> is blank (an error,\nbecause all fields are required by default) and <code class=\"docutils literal notranslate\"><span class=\"pre\">sender</span></code> is not a valid\nemail address:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">data</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;message&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Hi there&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;sender&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;invalid email address&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;cc_myself&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span><span class=\"p\">}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">is_valid</span><span class=\"p\">()</span>\n<span class=\"go\">False</span>\n</code></pre></div>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.errors\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">errors</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.errors\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Access the <a class=\"reference internal\" href=\"#django.forms.Form.errors\" title=\"django.forms.Form.errors\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">errors</span></code></a> attribute to get a dictionary of error\nmessages:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">errors</span>\n<span class=\"go\">{&#39;sender&#39;: [&#39;Enter a valid email address.&#39;], &#39;subject&#39;: [&#39;This field is required.&#39;]}</span>\n</code></pre></div>\n<p>In this dictionary, the keys are the field names, and the values are lists of\nstrings representing the error messages. The error messages are stored\nin lists because a field can have multiple error messages.</p>\n<p>You can access <a class=\"reference internal\" href=\"#django.forms.Form.errors\" title=\"django.forms.Form.errors\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">errors</span></code></a> without having to call\n<a class=\"reference internal\" href=\"#django.forms.Form.is_valid\" title=\"django.forms.Form.is_valid\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">is_valid()</span></code></a> first. The form’s data will be validated the first time\neither you call <a class=\"reference internal\" href=\"#django.forms.Form.is_valid\" title=\"django.forms.Form.is_valid\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">is_valid()</span></code></a> or access <a class=\"reference internal\" href=\"#django.forms.Form.errors\" title=\"django.forms.Form.errors\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">errors</span></code></a>.</p>\n<p>The validation routines will only get called once, regardless of how many times\nyou access <a class=\"reference internal\" href=\"#django.forms.Form.errors\" title=\"django.forms.Form.errors\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">errors</span></code></a> or call <a class=\"reference internal\" href=\"#django.forms.Form.is_valid\" title=\"django.forms.Form.is_valid\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">is_valid()</span></code></a>. This means that\nif validation has side effects, those side effects will only be triggered once.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.errors.as_data\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.errors.</span></span><span class=\"sig-name descname\"><span class=\"pre\">as_data</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Form.errors.as_data\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Returns a <code class=\"docutils literal notranslate\"><span class=\"pre\">dict</span></code> that maps fields to their original <code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code>\ninstances.</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">errors</span><span class=\"o\">.</span><span class=\"n\">as_data</span><span class=\"p\">()</span>\n<span class=\"go\">{&#39;sender&#39;: [ValidationError([&#39;Enter a valid email address.&#39;])],</span>\n<span class=\"go\">&#39;subject&#39;: [ValidationError([&#39;This field is required.&#39;])]}</span>\n</code></pre></div>\n<p>Use this method anytime you need to identify an error by its <code class=\"docutils literal notranslate\"><span class=\"pre\">code</span></code>. This\nenables things like rewriting the error’s message or writing custom logic in a\nview when a given error is present. It can also be used to serialize the errors\nin a custom format (e.g. XML); for instance, <a class=\"reference internal\" href=\"#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\">as_json()</span></code></a>\nrelies on <code class=\"docutils literal notranslate\"><span class=\"pre\">as_data()</span></code>.</p>\n<p>The need for the <code class=\"docutils literal notranslate\"><span class=\"pre\">as_data()</span></code> method is due to backwards compatibility.\nPreviously <code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code> instances were lost as soon as their\n<strong>rendered</strong> error messages were added to the <code class=\"docutils literal notranslate\"><span class=\"pre\">Form.errors</span></code> dictionary.\nIdeally <code class=\"docutils literal notranslate\"><span class=\"pre\">Form.errors</span></code> would have stored <code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code> instances\nand methods with an <code class=\"docutils literal notranslate\"><span class=\"pre\">as_</span></code> prefix could render them, but it had to be done\nthe other way around in order not to break code that expects rendered error\nmessages in <code class=\"docutils literal notranslate\"><span class=\"pre\">Form.errors</span></code>.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.errors.as_json\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.errors.</span></span><span class=\"sig-name descname\"><span class=\"pre\">as_json</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">escape_html</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Form.errors.as_json\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Returns the errors serialized as JSON.</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">errors</span><span class=\"o\">.</span><span class=\"n\">as_json</span><span class=\"p\">()</span>\n<span class=\"go\">{&quot;sender&quot;: [{&quot;message&quot;: &quot;Enter a valid email address.&quot;, &quot;code&quot;: &quot;invalid&quot;}],</span>\n<span class=\"go\">&quot;subject&quot;: [{&quot;message&quot;: &quot;This field is required.&quot;, &quot;code&quot;: &quot;required&quot;}]}</span>\n</code></pre></div>\n<p>By default, <code class=\"docutils literal notranslate\"><span class=\"pre\">as_json()</span></code> does not escape its output. If you are using it for\nsomething like AJAX requests to a form view where the client interprets the\nresponse and inserts errors into the page, you’ll want to be sure to escape the\nresults on the client-side to avoid the possibility of a cross-site scripting\nattack. You can do this in JavaScript with <code class=\"docutils literal notranslate\"><span class=\"pre\">element.textContent</span> <span class=\"pre\">=</span> <span class=\"pre\">errorText</span></code>\nor with jQuery’s <code class=\"docutils literal notranslate\"><span class=\"pre\">$(el).text(errorText)</span></code> (rather than its <code class=\"docutils literal notranslate\"><span class=\"pre\">.html()</span></code>\nfunction).</p>\n<p>If for some reason you don’t want to use client-side escaping, you can also\nset <code class=\"docutils literal notranslate\"><span class=\"pre\">escape_html=True</span></code> and error messages will be escaped so you can use them\ndirectly in HTML.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.errors.get_json_data\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.errors.</span></span><span class=\"sig-name descname\"><span class=\"pre\">get_json_data</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">escape_html</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Form.errors.get_json_data\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Returns the errors as a dictionary suitable for serializing to JSON.\n<a class=\"reference internal\" href=\"#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> returns serialized JSON, while this returns the\nerror data before it’s serialized.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">escape_html</span></code> parameter behaves as described in\n<a class=\"reference internal\" href=\"#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>.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.add_error\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">add_error</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">field</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">error</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Form.add_error\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>This method allows adding errors to specific fields from within the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Form.clean()</span></code> method, or from outside the form altogether; for instance\nfrom a view.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">field</span></code> argument is the name of the field to which the errors\nshould be added. If its value is <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code> the error will be treated as\na non-field error as returned by <a class=\"reference internal\" href=\"#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\">Form.non_field_errors()</span></code></a>.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">error</span></code> argument can be a string, or preferably an instance of\n<code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code>. See <a class=\"reference internal\" href=\"/pl/5.1/ref/forms/validation/#raising-validation-error\"><span class=\"std std-ref\">Raising ValidationError</span></a> for best practices\nwhen defining form errors.</p>\n<p>Note that <code class=\"docutils literal notranslate\"><span class=\"pre\">Form.add_error()</span></code> automatically removes the relevant field from\n<code class=\"docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code>.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.has_error\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">has_error</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">field</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">code</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Form.has_error\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>This method returns a boolean designating whether a field has an error with\na specific error <code class=\"docutils literal notranslate\"><span class=\"pre\">code</span></code>. If <code class=\"docutils literal notranslate\"><span class=\"pre\">code</span></code> is <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code>, it will return <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>\nif the field contains any errors at all.</p>\n<p>To check for non-field errors use\n<a class=\"reference internal\" href=\"/pl/5.1/ref/exceptions/#django.core.exceptions.NON_FIELD_ERRORS\" title=\"django.core.exceptions.NON_FIELD_ERRORS\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">NON_FIELD_ERRORS</span></code></a> as the <code class=\"docutils literal notranslate\"><span class=\"pre\">field</span></code> parameter.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.non_field_errors\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">non_field_errors</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Form.non_field_errors\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>This method returns the list of errors from <a class=\"reference internal\" href=\"#django.forms.Form.errors\" title=\"django.forms.Form.errors\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Form.errors</span></code></a>  that aren’t associated with a particular field.\nThis includes <code class=\"docutils literal notranslate\"><span class=\"pre\">ValidationError</span></code>s that are raised in <a class=\"reference internal\" href=\"#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> and errors added using <a class=\"reference internal\" href=\"#django.forms.Form.add_error\" title=\"django.forms.Form.add_error\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Form.add_error(None,</span>\n<span class=\"pre\">&quot;...&quot;)</span></code></a>.</p>\n<section id=\"behavior-of-unbound-forms\">\n<h3>Behavior of unbound forms<a class=\"heading-anchor\" href=\"#behavior-of-unbound-forms\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>It’s meaningless to validate a form with no data, but, for the record, here’s\nwhat happens with unbound forms:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">is_valid</span><span class=\"p\">()</span>\n<span class=\"go\">False</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">errors</span>\n<span class=\"go\">{}</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"initial-form-values\">\n<span id=\"ref-forms-initial-form-values\"></span><h2>Initial form values<a class=\"heading-anchor\" href=\"#initial-form-values\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.initial\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">initial</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.initial\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Use <a class=\"reference internal\" href=\"#django.forms.Form.initial\" title=\"django.forms.Form.initial\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">initial</span></code></a> to declare the initial value of form fields at\nruntime. For example, you might want to fill in a <code class=\"docutils literal notranslate\"><span class=\"pre\">username</span></code> field with the\nusername of the current session.</p>\n<p>To accomplish this, use the <a class=\"reference internal\" href=\"#django.forms.Form.initial\" title=\"django.forms.Form.initial\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">initial</span></code></a> argument to a <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a>.\nThis argument, if given, should be a dictionary mapping field names to initial\nvalues. Only include the fields for which you’re specifying an initial value;\nit’s not necessary to include every field in your form. For example:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">initial</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Hi there!&quot;</span><span class=\"p\">})</span>\n</code></pre></div>\n<p>These values are only displayed for unbound forms, and they’re not used as\nfallback values if a particular value isn’t provided.</p>\n<p>If a <a class=\"reference internal\" href=\"/pl/5.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> defines <a class=\"reference internal\" href=\"/pl/5.1/ref/forms/fields/#django.forms.Field.initial\" title=\"django.forms.Field.initial\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">initial</span></code></a> <em>and</em> you\ninclude <a class=\"reference internal\" href=\"#django.forms.Form.initial\" title=\"django.forms.Form.initial\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">initial</span></code></a> when instantiating the <code class=\"docutils literal notranslate\"><span class=\"pre\">Form</span></code>, then the latter\n<code class=\"docutils literal notranslate\"><span class=\"pre\">initial</span></code> will have precedence. In this example, <code class=\"docutils literal notranslate\"><span class=\"pre\">initial</span></code> is provided both\nat the field level and at the form instance level, and the latter gets\nprecedence:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><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=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">CommentForm</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=\"gp\">... </span>    <span class=\"n\">name</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\">initial</span><span class=\"o\">=</span><span class=\"s2\">&quot;class&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">... </span>    <span class=\"n\">url</span> <span class=\"o\">=</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">URLField</span><span class=\"p\">()</span>\n<span class=\"gp\">... </span>    <span class=\"n\">comment</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=\"gp\">...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">CommentForm</span><span class=\"p\">(</span><span class=\"n\">initial</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;name&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;instance&quot;</span><span class=\"p\">},</span> <span class=\"n\">auto_id</span><span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;div&gt;Name:&lt;input type=&quot;text&quot; name=&quot;name&quot; value=&quot;instance&quot; required&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;Url:&lt;input type=&quot;url&quot; name=&quot;url&quot; required&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;Comment:&lt;input type=&quot;text&quot; name=&quot;comment&quot; required&gt;&lt;/div&gt;</span>\n</code></pre></div>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.get_initial_for_field\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">get_initial_for_field</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">field</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">field_name</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Form.get_initial_for_field\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Returns the initial data for a form field. It retrieves the data from\n<a class=\"reference internal\" href=\"#django.forms.Form.initial\" title=\"django.forms.Form.initial\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Form.initial</span></code></a> if present, otherwise trying <a class=\"reference internal\" href=\"/pl/5.1/ref/forms/fields/#django.forms.Field.initial\" title=\"django.forms.Field.initial\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Field.initial</span></code></a>.\nCallable values are evaluated.</p>\n<p>It is recommended to use <a class=\"reference internal\" href=\"#django.forms.BoundField.initial\" title=\"django.forms.BoundField.initial\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">BoundField.initial</span></code></a> over\n<a class=\"reference internal\" href=\"#django.forms.Form.get_initial_for_field\" title=\"django.forms.Form.get_initial_for_field\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">get_initial_for_field()</span></code></a> because <code class=\"docutils literal notranslate\"><span class=\"pre\">BoundField.initial</span></code> has a\nsimpler interface. Also, unlike <a class=\"reference internal\" href=\"#django.forms.Form.get_initial_for_field\" title=\"django.forms.Form.get_initial_for_field\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">get_initial_for_field()</span></code></a>,\n<a class=\"reference internal\" href=\"#django.forms.BoundField.initial\" title=\"django.forms.BoundField.initial\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">BoundField.initial</span></code></a> caches its values. This is useful especially when\ndealing with callables whose return values can change (e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">datetime.now</span></code> or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">uuid.uuid4</span></code>):</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">uuid</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">UUIDCommentForm</span><span class=\"p\">(</span><span class=\"n\">CommentForm</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"n\">identifier</span> <span class=\"o\">=</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">UUIDField</span><span class=\"p\">(</span><span class=\"n\">initial</span><span class=\"o\">=</span><span class=\"n\">uuid</span><span class=\"o\">.</span><span class=\"n\">uuid4</span><span class=\"p\">)</span>\n<span class=\"gp\">...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">UUIDCommentForm</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">get_initial_for_field</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">fields</span><span class=\"p\">[</span><span class=\"s2\">&quot;identifier&quot;</span><span class=\"p\">],</span> <span class=\"s2\">&quot;identifier&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">UUID(&#39;972ca9e4-7bfe-4f5b-af7d-07b3aa306334&#39;)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">get_initial_for_field</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">fields</span><span class=\"p\">[</span><span class=\"s2\">&quot;identifier&quot;</span><span class=\"p\">],</span> <span class=\"s2\">&quot;identifier&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">UUID(&#39;1b411fab-844e-4dec-bd4f-e9b0495f04d0&#39;)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"c1\"># Using BoundField.initial, for comparison</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;identifier&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">initial</span>\n<span class=\"go\">UUID(&#39;28a09c59-5f00-4ed9-9179-a3b074fa9c30&#39;)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;identifier&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">initial</span>\n<span class=\"go\">UUID(&#39;28a09c59-5f00-4ed9-9179-a3b074fa9c30&#39;)</span>\n</code></pre></div>\n</section>\n<section id=\"checking-which-form-data-has-changed\">\n<h2>Checking which form data has changed<a class=\"heading-anchor\" href=\"#checking-which-form-data-has-changed\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.has_changed\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">has_changed</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Form.has_changed\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Use the <code class=\"docutils literal notranslate\"><span class=\"pre\">has_changed()</span></code> method on your <code class=\"docutils literal notranslate\"><span class=\"pre\">Form</span></code> when you need to check if the\nform data has been changed from the initial data.</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">data</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;hello&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;message&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Hi there&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;sender&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;foo@example.com&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;cc_myself&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span><span class=\"p\">}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"p\">,</span> <span class=\"n\">initial</span><span class=\"o\">=</span><span class=\"n\">data</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">has_changed</span><span class=\"p\">()</span>\n<span class=\"go\">False</span>\n</code></pre></div>\n<p>When the form is submitted, we reconstruct it and provide the original data\nso that the comparison can be done:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">POST</span><span class=\"p\">,</span> <span class=\"n\">initial</span><span class=\"o\">=</span><span class=\"n\">data</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">has_changed</span><span class=\"p\">()</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">has_changed()</span></code> will be <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> if the data from <code class=\"docutils literal notranslate\"><span class=\"pre\">request.POST</span></code> differs\nfrom what was provided in <a class=\"reference internal\" href=\"#django.forms.Form.initial\" title=\"django.forms.Form.initial\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">initial</span></code></a> or <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code> otherwise. The\nresult is computed by calling <a class=\"reference internal\" href=\"/pl/5.1/ref/forms/fields/#django.forms.Field.has_changed\" title=\"django.forms.Field.has_changed\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Field.has_changed()</span></code></a> for each field in the\nform.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.changed_data\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">changed_data</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.changed_data\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">changed_data</span></code> attribute returns a list of the names of the fields whose\nvalues in the form’s bound data (usually <code class=\"docutils literal notranslate\"><span class=\"pre\">request.POST</span></code>) differ from what was\nprovided in <a class=\"reference internal\" href=\"#django.forms.Form.initial\" title=\"django.forms.Form.initial\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">initial</span></code></a>. It returns an empty list if no data differs.</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">POST</span><span class=\"p\">,</span> <span class=\"n\">initial</span><span class=\"o\">=</span><span class=\"n\">data</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">if</span> <span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">has_changed</span><span class=\"p\">():</span>\n<span class=\"gp\">... </span>    <span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"s2\">&quot;The following fields changed: </span><span class=\"si\">%s</span><span class=\"s2\">&quot;</span> <span class=\"o\">%</span> <span class=\"s2\">&quot;, &quot;</span><span class=\"o\">.</span><span class=\"n\">join</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">changed_data</span><span class=\"p\">))</span>\n<span class=\"gp\">...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">changed_data</span>\n<span class=\"go\">[&#39;subject&#39;, &#39;message&#39;]</span>\n</code></pre></div>\n</section>\n<section id=\"accessing-the-fields-from-the-form\">\n<h2>Accessing the fields from the form<a class=\"heading-anchor\" href=\"#accessing-the-fields-from-the-form\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.fields\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">fields</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.fields\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>You can access the fields of <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> instance from its <code class=\"docutils literal notranslate\"><span class=\"pre\">fields</span></code>\nattribute:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">for</span> <span class=\"n\">row</span> <span class=\"ow\">in</span> <span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">fields</span><span class=\"o\">.</span><span class=\"n\">values</span><span class=\"p\">():</span>\n<span class=\"gp\">... </span>    <span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">row</span><span class=\"p\">)</span>\n<span class=\"gp\">...</span>\n<span class=\"go\">&lt;django.forms.fields.CharField object at 0x7ffaac632510&gt;</span>\n<span class=\"go\">&lt;django.forms.fields.URLField object at 0x7ffaac632f90&gt;</span>\n<span class=\"go\">&lt;django.forms.fields.CharField object at 0x7ffaac3aa050&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">fields</span><span class=\"p\">[</span><span class=\"s2\">&quot;name&quot;</span><span class=\"p\">]</span>\n<span class=\"go\">&lt;django.forms.fields.CharField object at 0x7ffaac6324d0&gt;</span>\n</code></pre></div>\n<p>You can alter the field and <a class=\"reference internal\" href=\"#django.forms.BoundField\" title=\"django.forms.BoundField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">BoundField</span></code></a> of <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> instance to\nchange the way it is presented in the form:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">as_div</span><span class=\"p\">()</span><span class=\"o\">.</span><span class=\"n\">split</span><span class=\"p\">(</span><span class=\"s2\">&quot;&lt;/div&gt;&quot;</span><span class=\"p\">)[</span><span class=\"mi\">0</span><span class=\"p\">]</span>\n<span class=\"go\">&#39;&lt;div&gt;&lt;label for=&quot;id_subject&quot;&gt;Subject:&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required id=&quot;id_subject&quot;&gt;&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">label</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;Topic&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">as_div</span><span class=\"p\">()</span><span class=\"o\">.</span><span class=\"n\">split</span><span class=\"p\">(</span><span class=\"s2\">&quot;&lt;/div&gt;&quot;</span><span class=\"p\">)[</span><span class=\"mi\">0</span><span class=\"p\">]</span>\n<span class=\"go\">&#39;&lt;div&gt;&lt;label for=&quot;id_subject&quot;&gt;Topic:&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required id=&quot;id_subject&quot;&gt;&#39;</span>\n</code></pre></div>\n<p>Beware not to alter the <code class=\"docutils literal notranslate\"><span class=\"pre\">base_fields</span></code> attribute because this modification\nwill influence all subsequent <code class=\"docutils literal notranslate\"><span class=\"pre\">ContactForm</span></code> instances within the same Python\nprocess:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">base_fields</span><span class=\"p\">[</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">label_suffix</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;?&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">another_f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">auto_id</span><span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">another_f</span><span class=\"o\">.</span><span class=\"n\">as_div</span><span class=\"p\">()</span><span class=\"o\">.</span><span class=\"n\">split</span><span class=\"p\">(</span><span class=\"s2\">&quot;&lt;/div&gt;&quot;</span><span class=\"p\">)[</span><span class=\"mi\">0</span><span class=\"p\">]</span>\n<span class=\"go\">&#39;&lt;div&gt;&lt;label for=&quot;id_subject&quot;&gt;Subject?&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required id=&quot;id_subject&quot;&gt;&#39;</span>\n</code></pre></div>\n</section>\n<section id=\"accessing-clean-data\">\n<h2>Accessing „clean” data<a class=\"heading-anchor\" href=\"#accessing-clean-data\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.cleaned_data\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">cleaned_data</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.cleaned_data\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Each field in a <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> class is responsible not only for validating\ndata, but also for „cleaning” it – normalizing it to a consistent format. This\nis a nice feature, because it allows data for a particular field to be input in\na variety of ways, always resulting in consistent output.</p>\n<p>For example, <a class=\"reference internal\" href=\"/pl/5.1/ref/forms/fields/#django.forms.DateField\" title=\"django.forms.DateField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">DateField</span></code></a> normalizes input into a\nPython <code class=\"docutils literal notranslate\"><span class=\"pre\">datetime.date</span></code> object. Regardless of whether you pass it a string in\nthe format <code class=\"docutils literal notranslate\"><span class=\"pre\">'1994-07-15'</span></code>, a <code class=\"docutils literal notranslate\"><span class=\"pre\">datetime.date</span></code> object, or a number of other\nformats, <code class=\"docutils literal notranslate\"><span class=\"pre\">DateField</span></code> will always normalize it to a <code class=\"docutils literal notranslate\"><span class=\"pre\">datetime.date</span></code> object\nas long as it’s valid.</p>\n<p>Once you’ve created a <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> instance with a set of data and validated\nit, you can access the clean data via its <code class=\"docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code> attribute:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">data</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;hello&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;message&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Hi there&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;sender&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;foo@example.com&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;cc_myself&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span><span class=\"p\">}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">is_valid</span><span class=\"p\">()</span>\n<span class=\"go\">True</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">cleaned_data</span>\n<span class=\"go\">{&#39;cc_myself&#39;: True, &#39;message&#39;: &#39;Hi there&#39;, &#39;sender&#39;: &#39;foo@example.com&#39;, &#39;subject&#39;: &#39;hello&#39;}</span>\n</code></pre></div>\n<p>Note that any text-based field – such as <code class=\"docutils literal notranslate\"><span class=\"pre\">CharField</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">EmailField</span></code> –\nalways cleans the input into a string. We’ll cover the encoding implications\nlater in this document.</p>\n<p>If your data does <em>not</em> validate, the <code class=\"docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code> dictionary contains\nonly the valid fields:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">data</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;message&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Hi there&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;sender&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;invalid email address&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;cc_myself&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span><span class=\"p\">}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">is_valid</span><span class=\"p\">()</span>\n<span class=\"go\">False</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">cleaned_data</span>\n<span class=\"go\">{&#39;cc_myself&#39;: True, &#39;message&#39;: &#39;Hi there&#39;}</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code> will always <em>only</em> contain a key for fields defined in the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Form</span></code>, even if you pass extra data when you define the <code class=\"docutils literal notranslate\"><span class=\"pre\">Form</span></code>. In this\nexample, we pass a bunch of extra fields to the <code class=\"docutils literal notranslate\"><span class=\"pre\">ContactForm</span></code> constructor,\nbut <code class=\"docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code> contains only the form’s fields:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">data</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;hello&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;message&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Hi there&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;sender&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;foo@example.com&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;cc_myself&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;extra_field_1&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;extra_field_2&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;bar&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;extra_field_3&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;baz&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span><span class=\"p\">}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">is_valid</span><span class=\"p\">()</span>\n<span class=\"go\">True</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">cleaned_data</span>  <span class=\"c1\"># Doesn&#39;t contain extra_field_1, etc.</span>\n<span class=\"go\">{&#39;cc_myself&#39;: True, &#39;message&#39;: &#39;Hi there&#39;, &#39;sender&#39;: &#39;foo@example.com&#39;, &#39;subject&#39;: &#39;hello&#39;}</span>\n</code></pre></div>\n<p>When the <code class=\"docutils literal notranslate\"><span class=\"pre\">Form</span></code> is valid, <code class=\"docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code> will include a key and value for\n<em>all</em> its fields, even if the data didn’t include a value for some optional\nfields. In this example, the data dictionary doesn’t include a value for the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">nick_name</span></code> field, but <code class=\"docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code> includes it, with an empty value:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><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=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">OptionalPersonForm</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=\"gp\">... </span>    <span class=\"n\">first_name</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=\"gp\">... </span>    <span class=\"n\">last_name</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=\"gp\">... </span>    <span class=\"n\">nick_name</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\">required</span><span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">)</span>\n<span class=\"gp\">...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">data</span> <span class=\"o\">=</span> <span class=\"p\">{</span><span class=\"s2\">&quot;first_name&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;John&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;last_name&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Lennon&quot;</span><span class=\"p\">}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">OptionalPersonForm</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">is_valid</span><span class=\"p\">()</span>\n<span class=\"go\">True</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">cleaned_data</span>\n<span class=\"go\">{&#39;nick_name&#39;: &#39;&#39;, &#39;first_name&#39;: &#39;John&#39;, &#39;last_name&#39;: &#39;Lennon&#39;}</span>\n</code></pre></div>\n<p>In this above example, the <code class=\"docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code> value for <code class=\"docutils literal notranslate\"><span class=\"pre\">nick_name</span></code> is set to an\nempty string, because <code class=\"docutils literal notranslate\"><span class=\"pre\">nick_name</span></code> is <code class=\"docutils literal notranslate\"><span class=\"pre\">CharField</span></code>, and <code class=\"docutils literal notranslate\"><span class=\"pre\">CharField</span></code>s treat\nempty values as an empty string. Each field type knows what its „blank” value\nis – e.g., for <code class=\"docutils literal notranslate\"><span class=\"pre\">DateField</span></code>, it’s <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code> instead of the empty string. For\nfull details on each field’s behavior in this case, see the „Empty value” note\nfor each field in the „Built-in <code class=\"docutils literal notranslate\"><span class=\"pre\">Field</span></code> classes” section below.</p>\n<p>You can write code to perform validation for particular form fields (based on\ntheir name) or for the form as a whole (considering combinations of various\nfields). More information about this is in <a class=\"reference internal\" href=\"/pl/5.1/ref/forms/validation/\"><span class=\"doc\">Form and field validation</span></a>.</p>\n</section>\n<section id=\"outputting-forms-as-html\">\n<span id=\"ref-forms-api-outputting-html\"></span><h2>Outputting forms as HTML<a class=\"heading-anchor\" href=\"#outputting-forms-as-html\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The second task of a <code class=\"docutils literal notranslate\"><span class=\"pre\">Form</span></code> object is to render itself as HTML. To do so,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">print</span></code> it:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_subject&quot;&gt;Subject:&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required id=&quot;id_subject&quot;&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_message&quot;&gt;Message:&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;message&quot; required id=&quot;id_message&quot;&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_sender&quot;&gt;Sender:&lt;/label&gt;&lt;input type=&quot;email&quot; name=&quot;sender&quot; required id=&quot;id_sender&quot;&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_cc_myself&quot;&gt;Cc myself:&lt;/label&gt;&lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot; id=&quot;id_cc_myself&quot;&gt;&lt;/div&gt;</span>\n</code></pre></div>\n<p>If the form is bound to data, the HTML output will include that data\nappropriately. For example, if a field is represented by an\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input</span> <span class=\"pre\">type=&quot;text&quot;&gt;</span></code>, the data will be in the <code class=\"docutils literal notranslate\"><span class=\"pre\">value</span></code> attribute. If a\nfield is represented by an <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input</span> <span class=\"pre\">type=&quot;checkbox&quot;&gt;</span></code>, then that HTML will\ninclude <code class=\"docutils literal notranslate\"><span class=\"pre\">checked</span></code> if appropriate:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">data</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;hello&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;message&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Hi there&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;sender&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;foo@example.com&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;cc_myself&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span><span class=\"p\">}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_subject&quot;&gt;Subject:&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;subject&quot; value=&quot;hello&quot; maxlength=&quot;100&quot; required id=&quot;id_subject&quot;&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_message&quot;&gt;Message:&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;message&quot; value=&quot;Hi there&quot; required id=&quot;id_message&quot;&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_sender&quot;&gt;Sender:&lt;/label&gt;&lt;input type=&quot;email&quot; name=&quot;sender&quot; value=&quot;foo@example.com&quot; required id=&quot;id_sender&quot;&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_cc_myself&quot;&gt;Cc myself:&lt;/label&gt;&lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot; id=&quot;id_cc_myself&quot; checked&gt;&lt;/div&gt;</span>\n</code></pre></div>\n<p>This default output wraps each field with a <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;div&gt;</span></code>. Notice the following:</p>\n<ul class=\"simple\">\n<li><p>For flexibility, the output does <em>not</em> include the <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;form&gt;</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;/form&gt;</span></code>\ntags or an <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input</span> <span class=\"pre\">type=&quot;submit&quot;&gt;</span></code> tag. It’s your job to do that.</p></li>\n<li><p>Each field type has a default HTML representation. <code class=\"docutils literal notranslate\"><span class=\"pre\">CharField</span></code> is\nrepresented by an <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input</span> <span class=\"pre\">type=&quot;text&quot;&gt;</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">EmailField</span></code> by an\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input</span> <span class=\"pre\">type=&quot;email&quot;&gt;</span></code>. <code class=\"docutils literal notranslate\"><span class=\"pre\">BooleanField(null=False)</span></code> is represented by an\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input</span> <span class=\"pre\">type=&quot;checkbox&quot;&gt;</span></code>. Note these are merely sensible defaults; you can\nspecify which HTML to use for a given field by using widgets, which we’ll\nexplain shortly.</p></li>\n<li><p>The HTML <code class=\"docutils literal notranslate\"><span class=\"pre\">name</span></code> for each tag is taken directly from its attribute name\nin the <code class=\"docutils literal notranslate\"><span class=\"pre\">ContactForm</span></code> class.</p></li>\n<li><p>The text label for each field – e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">'Subject:'</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">'Message:'</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'Cc</span> <span class=\"pre\">myself:'</span></code> is generated from the field name by converting all\nunderscores to spaces and upper-casing the first letter. Again, note\nthese are merely sensible defaults; you can also specify labels manually.</p></li>\n<li><p>Each text label is surrounded in an HTML <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code> tag, which points\nto the appropriate form field via its <code class=\"docutils literal notranslate\"><span class=\"pre\">id</span></code>. Its <code class=\"docutils literal notranslate\"><span class=\"pre\">id</span></code>, in turn, is\ngenerated by prepending <code class=\"docutils literal notranslate\"><span class=\"pre\">'id_'</span></code> to the field name. The <code class=\"docutils literal notranslate\"><span class=\"pre\">id</span></code>\nattributes and <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code> tags are included in the output by default, to\nfollow best practices, but you can change that behavior.</p></li>\n<li><p>The output uses HTML5 syntax, targeting <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;!DOCTYPE</span> <span class=\"pre\">html&gt;</span></code>. For example,\nit uses boolean attributes such as <code class=\"docutils literal notranslate\"><span class=\"pre\">checked</span></code> rather than the XHTML style\nof <code class=\"docutils literal notranslate\"><span class=\"pre\">checked='checked'</span></code>.</p></li>\n</ul>\n<p>Although <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;div&gt;</span></code> output is the default output style when you <code class=\"docutils literal notranslate\"><span class=\"pre\">print</span></code> a form\nyou can customize the output by using your own form template which can be set\nsite-wide, per-form, or per-instance. See <a class=\"reference internal\" href=\"/pl/5.1/topics/forms/#reusable-form-templates\"><span class=\"std std-ref\">Szablony formularzy do ponownego użycia</span></a>.</p>\n<section id=\"default-rendering\">\n<h3>Default rendering<a class=\"heading-anchor\" href=\"#default-rendering\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The default rendering when you <code class=\"docutils literal notranslate\"><span class=\"pre\">print</span></code> a form uses the following methods and\nattributes.</p>\n<section id=\"template-name\">\n<h4><code class=\"docutils literal notranslate\"><span class=\"pre\">template_name</span></code><a class=\"heading-anchor\" href=\"#template-name\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.template_name\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">template_name</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.template_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The name of the template rendered if the form is cast into a string, e.g. via\n<code class=\"docutils literal notranslate\"><span class=\"pre\">print(form)</span></code> or in a template via <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">form</span> <span class=\"pre\">}}</span></code>.</p>\n<p>By default, a property returning the value of the renderer’s\n<a class=\"reference internal\" href=\"/pl/5.1/ref/forms/renderers/#django.forms.renderers.BaseRenderer.form_template_name\" title=\"django.forms.renderers.BaseRenderer.form_template_name\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">form_template_name</span></code></a>. You may set it\nas a string template name in order to override that for a particular form\nclass.</p>\n</section>\n<section id=\"render\">\n<h4><code class=\"docutils literal notranslate\"><span class=\"pre\">render()</span></code><a class=\"heading-anchor\" href=\"#render\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.render\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">render</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">template_name</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">context</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">renderer</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Form.render\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The render method is called by <code class=\"docutils literal notranslate\"><span class=\"pre\">__str__</span></code> as well as the <a class=\"reference internal\" href=\"#django.forms.Form.as_div\" title=\"django.forms.Form.as_div\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Form.as_div()</span></code></a>,\n<a class=\"reference internal\" href=\"#django.forms.Form.as_table\" title=\"django.forms.Form.as_table\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Form.as_table()</span></code></a>, <a class=\"reference internal\" href=\"#django.forms.Form.as_p\" title=\"django.forms.Form.as_p\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Form.as_p()</span></code></a>, and <a class=\"reference internal\" href=\"#django.forms.Form.as_ul\" title=\"django.forms.Form.as_ul\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Form.as_ul()</span></code></a> methods.\nAll arguments are optional and default to:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">template_name</span></code>: <a class=\"reference internal\" href=\"#django.forms.Form.template_name\" title=\"django.forms.Form.template_name\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Form.template_name</span></code></a></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">context</span></code>: Value returned by <a class=\"reference internal\" href=\"#django.forms.Form.get_context\" title=\"django.forms.Form.get_context\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Form.get_context()</span></code></a></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">renderer</span></code>: Value returned by <a class=\"reference internal\" href=\"#django.forms.Form.default_renderer\" title=\"django.forms.Form.default_renderer\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Form.default_renderer</span></code></a></p></li>\n</ul>\n<p>By passing <code class=\"docutils literal notranslate\"><span class=\"pre\">template_name</span></code> you can customize the template used for just a\nsingle call.</p>\n</section>\n<section id=\"get-context\">\n<h4><code class=\"docutils literal notranslate\"><span class=\"pre\">get_context()</span></code><a class=\"heading-anchor\" href=\"#get-context\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.get_context\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">get_context</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Form.get_context\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Return the template context for rendering the form.</p>\n<p>The available context is:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">form</span></code>: The bound form.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">fields</span></code>: All bound fields, except the hidden fields.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">hidden_fields</span></code>: All hidden bound fields.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">errors</span></code>: All non field related or hidden field related form errors.</p></li>\n</ul>\n</section>\n<section id=\"template-name-label\">\n<h4><code class=\"docutils literal notranslate\"><span class=\"pre\">template_name_label</span></code><a class=\"heading-anchor\" href=\"#template-name-label\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.template_name_label\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">template_name_label</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.template_name_label\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The template used to render a field’s <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code>, used when calling\n<a class=\"reference internal\" href=\"#django.forms.BoundField.label_tag\" title=\"django.forms.BoundField.label_tag\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">BoundField.label_tag()</span></code></a>/<a class=\"reference internal\" href=\"#django.forms.BoundField.legend_tag\" title=\"django.forms.BoundField.legend_tag\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">legend_tag()</span></code></a>. Can be changed per\nform by overriding this attribute or more generally by overriding the default\ntemplate, see also <a class=\"reference internal\" href=\"/pl/5.1/ref/forms/renderers/#overriding-built-in-form-templates\"><span class=\"std std-ref\">Overriding built-in form templates</span></a>.</p>\n</section>\n</section>\n<section id=\"output-styles\">\n<h3>Output styles<a class=\"heading-anchor\" href=\"#output-styles\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The recommended approach for changing form output style is to set a custom form\ntemplate either site-wide, per-form, or per-instance. See\n<a class=\"reference internal\" href=\"/pl/5.1/topics/forms/#reusable-form-templates\"><span class=\"std std-ref\">Szablony formularzy do ponownego użycia</span></a> for examples.</p>\n<p>The following helper functions are provided for backward compatibility and are\na proxy to <a class=\"reference internal\" href=\"#django.forms.Form.render\" title=\"django.forms.Form.render\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Form.render()</span></code></a> passing a particular <code class=\"docutils literal notranslate\"><span class=\"pre\">template_name</span></code> value.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Informacja</p>\n<p>Of the framework provided templates and output styles, the default\n<code class=\"docutils literal notranslate\"><span class=\"pre\">as_div()</span></code> is recommended over the <code class=\"docutils literal notranslate\"><span class=\"pre\">as_p()</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">as_table()</span></code>, and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">as_ul()</span></code> versions as the template implements <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;fieldset&gt;</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;legend&gt;</span></code> to group related inputs and is easier for screen reader users\nto navigate.</p>\n</aside>\n<p>Each helper pairs a form method with an attribute giving the appropriate\ntemplate name.</p>\n<section id=\"as-div\">\n<h4><code class=\"docutils literal notranslate\"><span class=\"pre\">as_div()</span></code><a class=\"heading-anchor\" href=\"#as-div\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.template_name_div\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">template_name_div</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.template_name_div\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The template used by <code class=\"docutils literal notranslate\"><span class=\"pre\">as_div()</span></code>. Default: <code class=\"docutils literal notranslate\"><span class=\"pre\">'django/forms/div.html'</span></code>.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.as_div\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">as_div</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Form.as_div\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">as_div()</span></code> renders the form as a series of <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;div&gt;</span></code> elements, with each\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;div&gt;</span></code> containing one field, such as:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">as_div</span><span class=\"p\">()</span>\n</code></pre></div>\n<p>… gives HTML like:</p>\n<div class=\"code-block\" data-language=\"html\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Html</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=\"Html code\"><code><span class=\"p\">&lt;</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">label</span> <span class=\"na\">for</span><span class=\"o\">=</span><span class=\"s\">&quot;id_subject&quot;</span><span class=\"p\">&gt;</span>Subject:<span class=\"p\">&lt;/</span><span class=\"nt\">label</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">input</span> <span class=\"na\">type</span><span class=\"o\">=</span><span class=\"s\">&quot;text&quot;</span> <span class=\"na\">name</span><span class=\"o\">=</span><span class=\"s\">&quot;subject&quot;</span> <span class=\"na\">maxlength</span><span class=\"o\">=</span><span class=\"s\">&quot;100&quot;</span> <span class=\"na\">required</span> <span class=\"na\">id</span><span class=\"o\">=</span><span class=\"s\">&quot;id_subject&quot;</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">label</span> <span class=\"na\">for</span><span class=\"o\">=</span><span class=\"s\">&quot;id_message&quot;</span><span class=\"p\">&gt;</span>Message:<span class=\"p\">&lt;/</span><span class=\"nt\">label</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">input</span> <span class=\"na\">type</span><span class=\"o\">=</span><span class=\"s\">&quot;text&quot;</span> <span class=\"na\">name</span><span class=\"o\">=</span><span class=\"s\">&quot;message&quot;</span> <span class=\"na\">required</span> <span class=\"na\">id</span><span class=\"o\">=</span><span class=\"s\">&quot;id_message&quot;</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">label</span> <span class=\"na\">for</span><span class=\"o\">=</span><span class=\"s\">&quot;id_sender&quot;</span><span class=\"p\">&gt;</span>Sender:<span class=\"p\">&lt;/</span><span class=\"nt\">label</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">input</span> <span class=\"na\">type</span><span class=\"o\">=</span><span class=\"s\">&quot;email&quot;</span> <span class=\"na\">name</span><span class=\"o\">=</span><span class=\"s\">&quot;sender&quot;</span> <span class=\"na\">required</span> <span class=\"na\">id</span><span class=\"o\">=</span><span class=\"s\">&quot;id_sender&quot;</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">label</span> <span class=\"na\">for</span><span class=\"o\">=</span><span class=\"s\">&quot;id_cc_myself&quot;</span><span class=\"p\">&gt;</span>Cc myself:<span class=\"p\">&lt;/</span><span class=\"nt\">label</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">input</span> <span class=\"na\">type</span><span class=\"o\">=</span><span class=\"s\">&quot;checkbox&quot;</span> <span class=\"na\">name</span><span class=\"o\">=</span><span class=\"s\">&quot;cc_myself&quot;</span> <span class=\"na\">id</span><span class=\"o\">=</span><span class=\"s\">&quot;id_cc_myself&quot;</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n</section>\n<section id=\"as-p\">\n<h4><code class=\"docutils literal notranslate\"><span class=\"pre\">as_p()</span></code><a class=\"heading-anchor\" href=\"#as-p\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.template_name_p\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">template_name_p</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.template_name_p\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The template used by <code class=\"docutils literal notranslate\"><span class=\"pre\">as_p()</span></code>. Default: <code class=\"docutils literal notranslate\"><span class=\"pre\">'django/forms/p.html'</span></code>.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.as_p\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">as_p</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Form.as_p\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">as_p()</span></code> renders the form as a series of <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;p&gt;</span></code> tags, with each <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;p&gt;</span></code>\ncontaining one field:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">as_p</span><span class=\"p\">()</span>\n<span class=\"go\">&#39;&lt;p&gt;&lt;label for=&quot;id_subject&quot;&gt;Subject:&lt;/label&gt; &lt;input id=&quot;id_subject&quot; type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required&gt;&lt;/p&gt;\\n&lt;p&gt;&lt;label for=&quot;id_message&quot;&gt;Message:&lt;/label&gt; &lt;input type=&quot;text&quot; name=&quot;message&quot; id=&quot;id_message&quot; required&gt;&lt;/p&gt;\\n&lt;p&gt;&lt;label for=&quot;id_sender&quot;&gt;Sender:&lt;/label&gt; &lt;input type=&quot;text&quot; name=&quot;sender&quot; id=&quot;id_sender&quot; required&gt;&lt;/p&gt;\\n&lt;p&gt;&lt;label for=&quot;id_cc_myself&quot;&gt;Cc myself:&lt;/label&gt; &lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot; id=&quot;id_cc_myself&quot;&gt;&lt;/p&gt;&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">as_p</span><span class=\"p\">())</span>\n<span class=\"go\">&lt;p&gt;&lt;label for=&quot;id_subject&quot;&gt;Subject:&lt;/label&gt; &lt;input id=&quot;id_subject&quot; type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required&gt;&lt;/p&gt;</span>\n<span class=\"go\">&lt;p&gt;&lt;label for=&quot;id_message&quot;&gt;Message:&lt;/label&gt; &lt;input type=&quot;text&quot; name=&quot;message&quot; id=&quot;id_message&quot; required&gt;&lt;/p&gt;</span>\n<span class=\"go\">&lt;p&gt;&lt;label for=&quot;id_sender&quot;&gt;Sender:&lt;/label&gt; &lt;input type=&quot;email&quot; name=&quot;sender&quot; id=&quot;id_sender&quot; required&gt;&lt;/p&gt;</span>\n<span class=\"go\">&lt;p&gt;&lt;label for=&quot;id_cc_myself&quot;&gt;Cc myself:&lt;/label&gt; &lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot; id=&quot;id_cc_myself&quot;&gt;&lt;/p&gt;</span>\n</code></pre></div>\n</section>\n<section id=\"as-ul\">\n<h4><code class=\"docutils literal notranslate\"><span class=\"pre\">as_ul()</span></code><a class=\"heading-anchor\" href=\"#as-ul\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.template_name_ul\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">template_name_ul</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.template_name_ul\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The template used by <code class=\"docutils literal notranslate\"><span class=\"pre\">as_ul()</span></code>. Default: <code class=\"docutils literal notranslate\"><span class=\"pre\">'django/forms/ul.html'</span></code>.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.as_ul\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">as_ul</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Form.as_ul\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">as_ul()</span></code> renders the form as a series of <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;li&gt;</span></code> tags, with each <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;li&gt;</span></code>\ncontaining one field. It does <em>not</em> include the <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;ul&gt;</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;/ul&gt;</span></code>, so that\nyou can specify any HTML attributes on the <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;ul&gt;</span></code> for flexibility:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">as_ul</span><span class=\"p\">()</span>\n<span class=\"go\">&#39;&lt;li&gt;&lt;label for=&quot;id_subject&quot;&gt;Subject:&lt;/label&gt; &lt;input id=&quot;id_subject&quot; type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required&gt;&lt;/li&gt;\\n&lt;li&gt;&lt;label for=&quot;id_message&quot;&gt;Message:&lt;/label&gt; &lt;input type=&quot;text&quot; name=&quot;message&quot; id=&quot;id_message&quot; required&gt;&lt;/li&gt;\\n&lt;li&gt;&lt;label for=&quot;id_sender&quot;&gt;Sender:&lt;/label&gt; &lt;input type=&quot;email&quot; name=&quot;sender&quot; id=&quot;id_sender&quot; required&gt;&lt;/li&gt;\\n&lt;li&gt;&lt;label for=&quot;id_cc_myself&quot;&gt;Cc myself:&lt;/label&gt; &lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot; id=&quot;id_cc_myself&quot;&gt;&lt;/li&gt;&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">as_ul</span><span class=\"p\">())</span>\n<span class=\"go\">&lt;li&gt;&lt;label for=&quot;id_subject&quot;&gt;Subject:&lt;/label&gt; &lt;input id=&quot;id_subject&quot; type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required&gt;&lt;/li&gt;</span>\n<span class=\"go\">&lt;li&gt;&lt;label for=&quot;id_message&quot;&gt;Message:&lt;/label&gt; &lt;input type=&quot;text&quot; name=&quot;message&quot; id=&quot;id_message&quot; required&gt;&lt;/li&gt;</span>\n<span class=\"go\">&lt;li&gt;&lt;label for=&quot;id_sender&quot;&gt;Sender:&lt;/label&gt; &lt;input type=&quot;email&quot; name=&quot;sender&quot; id=&quot;id_sender&quot; required&gt;&lt;/li&gt;</span>\n<span class=\"go\">&lt;li&gt;&lt;label for=&quot;id_cc_myself&quot;&gt;Cc myself:&lt;/label&gt; &lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot; id=&quot;id_cc_myself&quot;&gt;&lt;/li&gt;</span>\n</code></pre></div>\n</section>\n<section id=\"as-table\">\n<h4><code class=\"docutils literal notranslate\"><span class=\"pre\">as_table()</span></code><a class=\"heading-anchor\" href=\"#as-table\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.template_name_table\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">template_name_table</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.template_name_table\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The template used by <code class=\"docutils literal notranslate\"><span class=\"pre\">as_table()</span></code>. Default: <code class=\"docutils literal notranslate\"><span class=\"pre\">'django/forms/table.html'</span></code>.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.as_table\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">as_table</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Form.as_table\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">as_table()</span></code> renders the form as an HTML <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;table&gt;</span></code>:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">as_table</span><span class=\"p\">()</span>\n<span class=\"go\">&#39;&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_subject&quot;&gt;Subject:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input id=&quot;id_subject&quot; type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required&gt;&lt;/td&gt;&lt;/tr&gt;\\n&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_message&quot;&gt;Message:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;message&quot; id=&quot;id_message&quot; required&gt;&lt;/td&gt;&lt;/tr&gt;\\n&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_sender&quot;&gt;Sender:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;email&quot; name=&quot;sender&quot; id=&quot;id_sender&quot; required&gt;&lt;/td&gt;&lt;/tr&gt;\\n&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_cc_myself&quot;&gt;Cc myself:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot; id=&quot;id_cc_myself&quot;&gt;&lt;/td&gt;&lt;/tr&gt;&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">as_table</span><span class=\"p\">())</span>\n<span class=\"go\">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_subject&quot;&gt;Subject:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input id=&quot;id_subject&quot; type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required&gt;&lt;/td&gt;&lt;/tr&gt;</span>\n<span class=\"go\">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_message&quot;&gt;Message:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;message&quot; id=&quot;id_message&quot; required&gt;&lt;/td&gt;&lt;/tr&gt;</span>\n<span class=\"go\">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_sender&quot;&gt;Sender:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;email&quot; name=&quot;sender&quot; id=&quot;id_sender&quot; required&gt;&lt;/td&gt;&lt;/tr&gt;</span>\n<span class=\"go\">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_cc_myself&quot;&gt;Cc myself:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot; id=&quot;id_cc_myself&quot;&gt;&lt;/td&gt;&lt;/tr&gt;</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"styling-required-or-erroneous-form-rows\">\n<span id=\"ref-forms-api-styling-form-rows\"></span><h3>Styling required or erroneous form rows<a class=\"heading-anchor\" href=\"#styling-required-or-erroneous-form-rows\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.error_css_class\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">error_css_class</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.error_css_class\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.required_css_class\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">required_css_class</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.required_css_class\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>It’s pretty common to style form rows and fields that are required or have\nerrors. For example, you might want to present required form rows in bold and\nhighlight errors in red.</p>\n<p>The <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> class has a couple of hooks you can use to add <code class=\"docutils literal notranslate\"><span class=\"pre\">class</span></code>\nattributes to required rows or to rows with errors: set the\n<a class=\"reference internal\" href=\"#django.forms.Form.error_css_class\" title=\"django.forms.Form.error_css_class\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Form.error_css_class</span></code></a> and/or <a class=\"reference internal\" href=\"#django.forms.Form.required_css_class\" title=\"django.forms.Form.required_css_class\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Form.required_css_class</span></code></a>\nattributes:</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\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=\"n\">error_css_class</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;error&quot;</span>\n    <span class=\"n\">required_css_class</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;required&quot;</span>\n\n    <span class=\"c1\"># ... and the rest of your fields here</span>\n</code></pre></div>\n<p>Once you’ve done that, rows will be given <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;error&quot;</span></code> and/or <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;required&quot;</span></code>\nclasses, as needed. The HTML will look something like:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;div class=&quot;required&quot;&gt;&lt;label for=&quot;id_subject&quot; class=&quot;required&quot;&gt;Subject:&lt;/label&gt; ...</span>\n<span class=\"go\">&lt;div class=&quot;required&quot;&gt;&lt;label for=&quot;id_message&quot; class=&quot;required&quot;&gt;Message:&lt;/label&gt; ...</span>\n<span class=\"go\">&lt;div class=&quot;required&quot;&gt;&lt;label for=&quot;id_sender&quot; class=&quot;required&quot;&gt;Sender:&lt;/label&gt; ...</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_cc_myself&quot;&gt;Cc myself:&lt;/label&gt; ...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">label_tag</span><span class=\"p\">()</span>\n<span class=\"go\">&lt;label class=&quot;required&quot; for=&quot;id_subject&quot;&gt;Subject:&lt;/label&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">legend_tag</span><span class=\"p\">()</span>\n<span class=\"go\">&lt;legend class=&quot;required&quot; for=&quot;id_subject&quot;&gt;Subject:&lt;/legend&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">label_tag</span><span class=\"p\">(</span><span class=\"n\">attrs</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;class&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">})</span>\n<span class=\"go\">&lt;label for=&quot;id_subject&quot; class=&quot;foo required&quot;&gt;Subject:&lt;/label&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">legend_tag</span><span class=\"p\">(</span><span class=\"n\">attrs</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;class&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">})</span>\n<span class=\"go\">&lt;legend for=&quot;id_subject&quot; class=&quot;foo required&quot;&gt;Subject:&lt;/legend&gt;</span>\n</code></pre></div>\n</section>\n<section id=\"configuring-form-elements-html-id-attributes-and-label-tags\">\n<span id=\"ref-forms-api-configuring-label\"></span><h3>Configuring form elements» HTML <code class=\"docutils literal notranslate\"><span class=\"pre\">id</span></code> attributes and <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code> tags<a class=\"heading-anchor\" href=\"#configuring-form-elements-html-id-attributes-and-label-tags\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.auto_id\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">auto_id</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.auto_id\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>By default, the form rendering methods include:</p>\n<ul class=\"simple\">\n<li><p>HTML <code class=\"docutils literal notranslate\"><span class=\"pre\">id</span></code> attributes on the form elements.</p></li>\n<li><p>The corresponding <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code> tags around the labels. An HTML <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code> tag\ndesignates which label text is associated with which form element. This small\nenhancement makes forms more usable and more accessible to assistive devices.\nIt’s always a good idea to use <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code> tags.</p></li>\n</ul>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">id</span></code> attribute values are generated by prepending <code class=\"docutils literal notranslate\"><span class=\"pre\">id_</span></code> to the form\nfield names.  This behavior is configurable, though, if you want to change the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">id</span></code> convention or remove HTML <code class=\"docutils literal notranslate\"><span class=\"pre\">id</span></code> attributes and <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code> tags\nentirely.</p>\n<p>Use the <code class=\"docutils literal notranslate\"><span class=\"pre\">auto_id</span></code> argument to the <code class=\"docutils literal notranslate\"><span class=\"pre\">Form</span></code> constructor to control the <code class=\"docutils literal notranslate\"><span class=\"pre\">id</span></code>\nand label behavior. This argument must be <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code> or a string.</p>\n<p>If <code class=\"docutils literal notranslate\"><span class=\"pre\">auto_id</span></code> is <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code>, then the form output will not include <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code>\ntags nor <code class=\"docutils literal notranslate\"><span class=\"pre\">id</span></code> attributes:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">auto_id</span><span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;div&gt;Subject:&lt;input type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;Message:&lt;textarea name=&quot;message&quot; cols=&quot;40&quot; rows=&quot;10&quot; required&gt;&lt;/textarea&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;Sender:&lt;input type=&quot;email&quot; name=&quot;sender&quot; required&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;Cc myself:&lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot;&gt;&lt;/div&gt;</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\"><span class=\"pre\">auto_id</span></code> is set to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, then the form output <em>will</em> include\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code> tags and will use the field name as its <code class=\"docutils literal notranslate\"><span class=\"pre\">id</span></code> for each form\nfield:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">auto_id</span><span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;subject&quot;&gt;Subject:&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required id=&quot;subject&quot;&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;message&quot;&gt;Message:&lt;/label&gt;&lt;textarea name=&quot;message&quot; cols=&quot;40&quot; rows=&quot;10&quot; required id=&quot;message&quot;&gt;&lt;/textarea&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;sender&quot;&gt;Sender:&lt;/label&gt;&lt;input type=&quot;email&quot; name=&quot;sender&quot; required id=&quot;sender&quot;&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;cc_myself&quot;&gt;Cc myself:&lt;/label&gt;&lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot; id=&quot;cc_myself&quot;&gt;&lt;/div&gt;</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\"><span class=\"pre\">auto_id</span></code> is set to a string containing the format character <code class=\"docutils literal notranslate\"><span class=\"pre\">'%s'</span></code>,\nthen the form output will include <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code> tags, and will generate <code class=\"docutils literal notranslate\"><span class=\"pre\">id</span></code>\nattributes based on the format string. For example, for a format string\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'field_%s'</span></code>, a field named <code class=\"docutils literal notranslate\"><span class=\"pre\">subject</span></code> will get the <code class=\"docutils literal notranslate\"><span class=\"pre\">id</span></code> value\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'field_subject'</span></code>. Continuing our example:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">auto_id</span><span class=\"o\">=</span><span class=\"s2\">&quot;id_for_</span><span class=\"si\">%s</span><span class=\"s2\">&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_for_subject&quot;&gt;Subject:&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required id=&quot;id_for_subject&quot;&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_for_message&quot;&gt;Message:&lt;/label&gt;&lt;textarea name=&quot;message&quot; cols=&quot;40&quot; rows=&quot;10&quot; required id=&quot;id_for_message&quot;&gt;&lt;/textarea&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_for_sender&quot;&gt;Sender:&lt;/label&gt;&lt;input type=&quot;email&quot; name=&quot;sender&quot; required id=&quot;id_for_sender&quot;&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_for_cc_myself&quot;&gt;Cc myself:&lt;/label&gt;&lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot; id=&quot;id_for_cc_myself&quot;&gt;&lt;/div&gt;</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\"><span class=\"pre\">auto_id</span></code> is set to any other true value – such as a string that doesn’t\ninclude <code class=\"docutils literal notranslate\"><span class=\"pre\">%s</span></code> – then the library will act as if <code class=\"docutils literal notranslate\"><span class=\"pre\">auto_id</span></code> is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>.</p>\n<p>By default, <code class=\"docutils literal notranslate\"><span class=\"pre\">auto_id</span></code> is set to the string <code class=\"docutils literal notranslate\"><span class=\"pre\">'id_%s'</span></code>.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.label_suffix\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">label_suffix</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.label_suffix\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A translatable string (defaults to a colon (<code class=\"docutils literal notranslate\"><span class=\"pre\">:</span></code>) in English) that will be\nappended after any label name when a form is rendered.</p>\n<p>It’s possible to customize that character, or omit it entirely, using the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">label_suffix</span></code> parameter:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">auto_id</span><span class=\"o\">=</span><span class=\"s2\">&quot;id_for_</span><span class=\"si\">%s</span><span class=\"s2\">&quot;</span><span class=\"p\">,</span> <span class=\"n\">label_suffix</span><span class=\"o\">=</span><span class=\"s2\">&quot;&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_for_subject&quot;&gt;Subject&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required id=&quot;id_for_subject&quot;&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_for_message&quot;&gt;Message&lt;/label&gt;&lt;textarea name=&quot;message&quot; cols=&quot;40&quot; rows=&quot;10&quot; required id=&quot;id_for_message&quot;&gt;&lt;/textarea&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_for_sender&quot;&gt;Sender&lt;/label&gt;&lt;input type=&quot;email&quot; name=&quot;sender&quot; required id=&quot;id_for_sender&quot;&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_for_cc_myself&quot;&gt;Cc myself&lt;/label&gt;&lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot; id=&quot;id_for_cc_myself&quot;&gt;&lt;/div&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">auto_id</span><span class=\"o\">=</span><span class=\"s2\">&quot;id_for_</span><span class=\"si\">%s</span><span class=\"s2\">&quot;</span><span class=\"p\">,</span> <span class=\"n\">label_suffix</span><span class=\"o\">=</span><span class=\"s2\">&quot; -&gt;&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_for_subject&quot;&gt;Subject -&amp;gt;&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required id=&quot;id_for_subject&quot;&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_for_message&quot;&gt;Message -&amp;gt;&lt;/label&gt;&lt;textarea name=&quot;message&quot; cols=&quot;40&quot; rows=&quot;10&quot; required id=&quot;id_for_message&quot;&gt;&lt;/textarea&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_for_sender&quot;&gt;Sender -&amp;gt;&lt;/label&gt;&lt;input type=&quot;email&quot; name=&quot;sender&quot; required id=&quot;id_for_sender&quot;&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_for_cc_myself&quot;&gt;Cc myself -&amp;gt;&lt;/label&gt;&lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot; id=&quot;id_for_cc_myself&quot;&gt;&lt;/div&gt;</span>\n</code></pre></div>\n<p>Note that the label suffix is added only if the last character of the\nlabel isn’t a punctuation character (in English, those are <code class=\"docutils literal notranslate\"><span class=\"pre\">.</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">!</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">?</span></code>\nor <code class=\"docutils literal notranslate\"><span class=\"pre\">:</span></code>).</p>\n<p>Fields can also define their own <a class=\"reference internal\" href=\"/pl/5.1/ref/forms/fields/#django.forms.Field.label_suffix\" title=\"django.forms.Field.label_suffix\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">label_suffix</span></code></a>.\nThis will take precedence over <a class=\"reference internal\" href=\"#django.forms.Form.label_suffix\" title=\"django.forms.Form.label_suffix\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Form.label_suffix</span></code></a>. The suffix can also be overridden at runtime\nusing the <code class=\"docutils literal notranslate\"><span class=\"pre\">label_suffix</span></code> parameter to\n<a class=\"reference internal\" href=\"#django.forms.BoundField.label_tag\" title=\"django.forms.BoundField.label_tag\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">label_tag()</span></code></a>/\n<a class=\"reference internal\" href=\"#django.forms.BoundField.legend_tag\" title=\"django.forms.BoundField.legend_tag\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">legend_tag()</span></code></a>.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.use_required_attribute\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">use_required_attribute</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.use_required_attribute\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>When set to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> (the default), required form fields will have the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">required</span></code> HTML attribute.</p>\n<p><a class=\"reference internal\" href=\"/pl/5.1/topics/forms/formsets/\"><span class=\"doc\">Formsets</span></a> instantiate forms with\n<code class=\"docutils literal notranslate\"><span class=\"pre\">use_required_attribute=False</span></code> to avoid incorrect browser validation when\nadding and deleting forms from a formset.</p>\n</section>\n<section id=\"configuring-the-rendering-of-a-form-s-widgets\">\n<h3>Configuring the rendering of a form’s widgets<a class=\"heading-anchor\" href=\"#configuring-the-rendering-of-a-form-s-widgets\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.default_renderer\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">default_renderer</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.default_renderer\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Specifies the <a class=\"reference internal\" href=\"/pl/5.1/ref/forms/renderers/\"><span class=\"doc\">renderer</span></a> to use for the form. Defaults to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code> which means to use the default renderer specified by the\n<a class=\"reference internal\" href=\"/pl/5.1/ref/settings/#std-setting-FORM_RENDERER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">FORM_RENDERER</span></code></a> setting.</p>\n<p>You can set this as a class attribute when declaring your form or use the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">renderer</span></code> argument to <code class=\"docutils literal notranslate\"><span class=\"pre\">Form.__init__()</span></code>. For example:</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\n<span class=\"k\">class</span><span class=\"w\"> </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>\n    <span class=\"n\">default_renderer</span> <span class=\"o\">=</span> <span class=\"n\">MyRenderer</span><span class=\"p\">()</span>\n</code></pre></div>\n<p>or:</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\">form</span> <span class=\"o\">=</span> <span class=\"n\">MyForm</span><span class=\"p\">(</span><span class=\"n\">renderer</span><span class=\"o\">=</span><span class=\"n\">MyRenderer</span><span class=\"p\">())</span>\n</code></pre></div>\n</section>\n<section id=\"notes-on-field-ordering\">\n<h3>Notes on field ordering<a class=\"heading-anchor\" href=\"#notes-on-field-ordering\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>In the <code class=\"docutils literal notranslate\"><span class=\"pre\">as_p()</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">as_ul()</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">as_table()</span></code> shortcuts, the fields are\ndisplayed in the order in which you define them in your form class. For\nexample, in the <code class=\"docutils literal notranslate\"><span class=\"pre\">ContactForm</span></code> example, the fields are defined in the order\n<code class=\"docutils literal notranslate\"><span class=\"pre\">subject</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">message</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">sender</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">cc_myself</span></code>. To reorder the HTML\noutput, change the order in which those fields are listed in the class.</p>\n<p>There are several other ways to customize the order:</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.field_order\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">field_order</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.field_order\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>By default <code class=\"docutils literal notranslate\"><span class=\"pre\">Form.field_order=None</span></code>, which retains the order in which you\ndefine the fields in your form class. If <code class=\"docutils literal notranslate\"><span class=\"pre\">field_order</span></code> is a list of field\nnames, the fields are ordered as specified by the list and remaining fields are\nappended according to the default order. Unknown field names in the list are\nignored. This makes it possible to disable a field in a subclass by setting it\nto <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code> without having to redefine ordering.</p>\n<p>You can also use the <code class=\"docutils literal notranslate\"><span class=\"pre\">Form.field_order</span></code> argument to a <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> to\noverride the field order. If a <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> defines\n<a class=\"reference internal\" href=\"#django.forms.Form.field_order\" title=\"django.forms.Form.field_order\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">field_order</span></code></a> <em>and</em> you include <code class=\"docutils literal notranslate\"><span class=\"pre\">field_order</span></code> when instantiating\nthe <code class=\"docutils literal notranslate\"><span class=\"pre\">Form</span></code>, then the latter <code class=\"docutils literal notranslate\"><span class=\"pre\">field_order</span></code> will have precedence.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.order_fields\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">order_fields</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">field_order</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Form.order_fields\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>You may rearrange the fields any time using <code class=\"docutils literal notranslate\"><span class=\"pre\">order_fields()</span></code> with a list of\nfield names as in <a class=\"reference internal\" href=\"#django.forms.Form.field_order\" title=\"django.forms.Form.field_order\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">field_order</span></code></a>.</p>\n</section>\n<section id=\"how-errors-are-displayed\">\n<h3>How errors are displayed<a class=\"heading-anchor\" href=\"#how-errors-are-displayed\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you render a bound <code class=\"docutils literal notranslate\"><span class=\"pre\">Form</span></code> object, the act of rendering will automatically\nrun the form’s validation if it hasn’t already happened, and the HTML output\nwill include the validation errors as a <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;ul</span> <span class=\"pre\">class=&quot;errorlist&quot;&gt;</span></code> near the\nfield. The particular positioning of the error messages depends on the output\nmethod you’re using:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">data</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;message&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Hi there&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;sender&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;invalid email address&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;cc_myself&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span><span class=\"p\">}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"p\">,</span> <span class=\"n\">auto_id</span><span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;div&gt;Subject:</span>\n<span class=\"go\">  &lt;ul class=&quot;errorlist&quot;&gt;&lt;li&gt;This field is required.&lt;/li&gt;&lt;/ul&gt;</span>\n<span class=\"go\">  &lt;input type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required aria-invalid=&quot;true&quot;&gt;</span>\n<span class=\"go\">&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;Message:</span>\n<span class=\"go\">  &lt;textarea name=&quot;message&quot; cols=&quot;40&quot; rows=&quot;10&quot; required&gt;Hi there&lt;/textarea&gt;</span>\n<span class=\"go\">&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;Sender:</span>\n<span class=\"go\">  &lt;ul class=&quot;errorlist&quot;&gt;&lt;li&gt;Enter a valid email address.&lt;/li&gt;&lt;/ul&gt;</span>\n<span class=\"go\">  &lt;input type=&quot;email&quot; name=&quot;sender&quot; value=&quot;invalid email address&quot; required aria-invalid=&quot;true&quot;&gt;</span>\n<span class=\"go\">&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;Cc myself:</span>\n<span class=\"go\">  &lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot; checked&gt;</span>\n<span class=\"go\">&lt;/div&gt;</span>\n</code></pre></div>\n</section>\n<section id=\"customizing-the-error-list-format\">\n<span id=\"ref-forms-error-list-format\"></span><h3>Customizing the error list format<a class=\"heading-anchor\" href=\"#customizing-the-error-list-format\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.forms.ErrorList\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">ErrorList</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">initlist</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">error_class</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">renderer</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.ErrorList\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>By default, forms use <code class=\"docutils literal notranslate\"><span class=\"pre\">django.forms.utils.ErrorList</span></code> to format validation\nerrors. <code class=\"docutils literal notranslate\"><span class=\"pre\">ErrorList</span></code> is a list like object where <code class=\"docutils literal notranslate\"><span class=\"pre\">initlist</span></code> is the\nlist of errors. In addition this class has the following attributes and\nmethods.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.ErrorList.error_class\">\n<span class=\"sig-name descname\"><span class=\"pre\">error_class</span></span><a class=\"heading-anchor\" href=\"#django.forms.ErrorList.error_class\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The CSS classes to be used when rendering the error list. Any provided\nclasses are added to the default <code class=\"docutils literal notranslate\"><span class=\"pre\">errorlist</span></code> class.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.ErrorList.renderer\">\n<span class=\"sig-name descname\"><span class=\"pre\">renderer</span></span><a class=\"heading-anchor\" href=\"#django.forms.ErrorList.renderer\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Specifies the <a class=\"reference internal\" href=\"/pl/5.1/ref/forms/renderers/\"><span class=\"doc\">renderer</span></a> to use for <code class=\"docutils literal notranslate\"><span class=\"pre\">ErrorList</span></code>.\nDefaults to <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code> which means to use the default renderer\nspecified by the <a class=\"reference internal\" href=\"/pl/5.1/ref/settings/#std-setting-FORM_RENDERER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">FORM_RENDERER</span></code></a> setting.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.ErrorList.template_name\">\n<span class=\"sig-name descname\"><span class=\"pre\">template_name</span></span><a class=\"heading-anchor\" href=\"#django.forms.ErrorList.template_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The name of the template used when calling <code class=\"docutils literal notranslate\"><span class=\"pre\">__str__</span></code> or\n<a class=\"reference internal\" href=\"#django.forms.ErrorList.render\" title=\"django.forms.ErrorList.render\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">render()</span></code></a>. By default this is\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'django/forms/errors/list/default.html'</span></code> which is a proxy for the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'ul.html'</span></code> template.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.ErrorList.template_name_text\">\n<span class=\"sig-name descname\"><span class=\"pre\">template_name_text</span></span><a class=\"heading-anchor\" href=\"#django.forms.ErrorList.template_name_text\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The name of the template used when calling <a class=\"reference internal\" href=\"#django.forms.ErrorList.as_text\" title=\"django.forms.ErrorList.as_text\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">as_text()</span></code></a>. By default\nthis is <code class=\"docutils literal notranslate\"><span class=\"pre\">'django/forms/errors/list/text.html'</span></code>. This template renders\nthe errors as a list of bullet points.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.ErrorList.template_name_ul\">\n<span class=\"sig-name descname\"><span class=\"pre\">template_name_ul</span></span><a class=\"heading-anchor\" href=\"#django.forms.ErrorList.template_name_ul\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The name of the template used when calling <a class=\"reference internal\" href=\"#django.forms.ErrorList.as_ul\" title=\"django.forms.ErrorList.as_ul\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">as_ul()</span></code></a>. By default\nthis is <code class=\"docutils literal notranslate\"><span class=\"pre\">'django/forms/errors/list/ul.html'</span></code>. This template renders\nthe errors in <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;li&gt;</span></code> tags with a wrapping <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;ul&gt;</span></code> with the CSS\nclasses as defined by <a class=\"reference internal\" href=\"#django.forms.ErrorList.error_class\" title=\"django.forms.ErrorList.error_class\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">error_class</span></code></a>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.ErrorList.get_context\">\n<span class=\"sig-name descname\"><span class=\"pre\">get_context</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.ErrorList.get_context\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Return context for rendering of errors in a template.</p>\n<p>The available context is:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">errors</span></code> : A list of the errors.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">error_class</span></code> : A string of CSS classes.</p></li>\n</ul>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.ErrorList.render\">\n<span class=\"sig-name descname\"><span class=\"pre\">render</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">template_name</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">context</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">renderer</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.ErrorList.render\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The render method is called by <code class=\"docutils literal notranslate\"><span class=\"pre\">__str__</span></code> as well as by the\n<a class=\"reference internal\" href=\"#django.forms.ErrorList.as_ul\" title=\"django.forms.ErrorList.as_ul\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">as_ul()</span></code></a> method.</p>\n<p>All arguments are optional and will default to:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">template_name</span></code>: Value returned by <a class=\"reference internal\" href=\"#django.forms.ErrorList.template_name\" title=\"django.forms.ErrorList.template_name\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">template_name</span></code></a></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">context</span></code>: Value returned by <a class=\"reference internal\" href=\"#django.forms.ErrorList.get_context\" title=\"django.forms.ErrorList.get_context\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">get_context()</span></code></a></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">renderer</span></code>: Value returned by <a class=\"reference internal\" href=\"#django.forms.ErrorList.renderer\" title=\"django.forms.ErrorList.renderer\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">renderer</span></code></a></p></li>\n</ul>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.ErrorList.as_text\">\n<span class=\"sig-name descname\"><span class=\"pre\">as_text</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.ErrorList.as_text\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Renders the error list using the template defined by\n<a class=\"reference internal\" href=\"#django.forms.ErrorList.template_name_text\" title=\"django.forms.ErrorList.template_name_text\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">template_name_text</span></code></a>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.ErrorList.as_ul\">\n<span class=\"sig-name descname\"><span class=\"pre\">as_ul</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.ErrorList.as_ul\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Renders the error list using the template defined by\n<a class=\"reference internal\" href=\"#django.forms.ErrorList.template_name_ul\" title=\"django.forms.ErrorList.template_name_ul\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">template_name_ul</span></code></a>.</p>\n</dd></dl>\n\n<p>If you’d like to customize the rendering of errors this can be achieved by\noverriding the <a class=\"reference internal\" href=\"#django.forms.ErrorList.template_name\" title=\"django.forms.ErrorList.template_name\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">template_name</span></code></a> attribute or more generally by\noverriding the default template, see also\n<a class=\"reference internal\" href=\"/pl/5.1/ref/forms/renderers/#overriding-built-in-form-templates\"><span class=\"std std-ref\">Overriding built-in form templates</span></a>.</p>\n</dd></dl>\n\n</section>\n</section>\n<section id=\"more-granular-output\">\n<h2>More granular output<a class=\"heading-anchor\" href=\"#more-granular-output\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">as_p()</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">as_ul()</span></code>, and <code class=\"docutils literal notranslate\"><span class=\"pre\">as_table()</span></code> methods are shortcuts –\nthey’re not the only way a form object can be displayed.</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">BoundField</span></span><a class=\"heading-anchor\" href=\"#django.forms.BoundField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Used to display HTML or access attributes for a single field of a\n<a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> instance.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">__str__()</span></code> method of this object displays the HTML for this field.</p>\n</dd></dl>\n\n<p>To retrieve a single <code class=\"docutils literal notranslate\"><span class=\"pre\">BoundField</span></code>, use dictionary lookup syntax on your form\nusing the field’s name as the key:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">form</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">form</span><span class=\"p\">[</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">])</span>\n<span class=\"go\">&lt;input id=&quot;id_subject&quot; type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required&gt;</span>\n</code></pre></div>\n<p>To retrieve all <code class=\"docutils literal notranslate\"><span class=\"pre\">BoundField</span></code> objects, iterate the form:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">form</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">for</span> <span class=\"n\">boundfield</span> <span class=\"ow\">in</span> <span class=\"n\">form</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>    <span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">boundfield</span><span class=\"p\">)</span>\n<span class=\"gp\">...</span>\n<span class=\"go\">&lt;input id=&quot;id_subject&quot; type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required&gt;</span>\n<span class=\"go\">&lt;input type=&quot;text&quot; name=&quot;message&quot; id=&quot;id_message&quot; required&gt;</span>\n<span class=\"go\">&lt;input type=&quot;email&quot; name=&quot;sender&quot; id=&quot;id_sender&quot; required&gt;</span>\n<span class=\"go\">&lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot; id=&quot;id_cc_myself&quot;&gt;</span>\n</code></pre></div>\n<p>The field-specific output honors the form object’s <code class=\"docutils literal notranslate\"><span class=\"pre\">auto_id</span></code> setting:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">auto_id</span><span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;message&quot;</span><span class=\"p\">])</span>\n<span class=\"go\">&lt;input type=&quot;text&quot; name=&quot;message&quot; required&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">auto_id</span><span class=\"o\">=</span><span class=\"s2\">&quot;id_</span><span class=\"si\">%s</span><span class=\"s2\">&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;message&quot;</span><span class=\"p\">])</span>\n<span class=\"go\">&lt;input type=&quot;text&quot; name=&quot;message&quot; id=&quot;id_message&quot; required&gt;</span>\n</code></pre></div>\n<section id=\"attributes-of-boundfield\">\n<h3>Attributes of <code class=\"docutils literal notranslate\"><span class=\"pre\">BoundField</span></code><a class=\"heading-anchor\" href=\"#attributes-of-boundfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.auto_id\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">auto_id</span></span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.auto_id\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The HTML ID attribute for this <code class=\"docutils literal notranslate\"><span class=\"pre\">BoundField</span></code>. Returns an empty string\nif <a class=\"reference internal\" href=\"#django.forms.Form.auto_id\" title=\"django.forms.Form.auto_id\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Form.auto_id</span></code></a> is <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.data\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">data</span></span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.data\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This property returns the data for this <a class=\"reference internal\" href=\"#django.forms.BoundField\" title=\"django.forms.BoundField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">BoundField</span></code></a>\nextracted by the widget’s <a class=\"reference internal\" href=\"/pl/5.1/ref/forms/widgets/#django.forms.Widget.value_from_datadict\" title=\"django.forms.Widget.value_from_datadict\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">value_from_datadict()</span></code></a>\nmethod, or <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code> if it wasn’t given:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">unbound_form</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">unbound_form</span><span class=\"p\">[</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">data</span><span class=\"p\">)</span>\n<span class=\"go\">None</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">bound_form</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;My Subject&quot;</span><span class=\"p\">})</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">bound_form</span><span class=\"p\">[</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">data</span><span class=\"p\">)</span>\n<span class=\"go\">My Subject</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.errors\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">errors</span></span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.errors\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A <a class=\"reference internal\" href=\"#ref-forms-error-list-format\"><span class=\"std std-ref\">list-like object</span></a> that is displayed\nas an HTML <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;ul</span> <span class=\"pre\">class=&quot;errorlist&quot;&gt;</span></code> when printed:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">data</span> <span class=\"o\">=</span> <span class=\"p\">{</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;hi&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;message&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;sender&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;cc_myself&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;&quot;</span><span class=\"p\">}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"p\">,</span> <span class=\"n\">auto_id</span><span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;message&quot;</span><span class=\"p\">])</span>\n<span class=\"go\">&lt;input type=&quot;text&quot; name=&quot;message&quot; required aria-invalid=&quot;true&quot;&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;message&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">errors</span>\n<span class=\"go\">[&#39;This field is required.&#39;]</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;message&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">errors</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;ul class=&quot;errorlist&quot;&gt;&lt;li&gt;This field is required.&lt;/li&gt;&lt;/ul&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">errors</span>\n<span class=\"go\">[]</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">errors</span><span class=\"p\">)</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">str</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">errors</span><span class=\"p\">)</span>\n<span class=\"go\">&#39;&#39;</span>\n</code></pre></div>\n<p>When rendering a field with errors, <code class=\"docutils literal notranslate\"><span class=\"pre\">aria-invalid=&quot;true&quot;</span></code> will be set on\nthe field’s widget to indicate there is an error to screen reader users.</p>\n<aside class=\"version-note version-changed\" data-version=\"5.0\">\n<p class=\"version-note-title\">Changed in Django 5.0</p><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">aria-invalid=&quot;true&quot;</span></code> was added when a field has errors.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.field\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">field</span></span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.field\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The form <a class=\"reference internal\" href=\"/pl/5.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> instance from the form class that\nthis <a class=\"reference internal\" href=\"#django.forms.BoundField\" title=\"django.forms.BoundField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">BoundField</span></code></a> wraps.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.form\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">form</span></span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.form\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> instance this <a class=\"reference internal\" href=\"#django.forms.BoundField\" title=\"django.forms.BoundField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">BoundField</span></code></a>\nis bound to.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.help_text\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">help_text</span></span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.help_text\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The <a class=\"reference internal\" href=\"/pl/5.1/ref/forms/fields/#django.forms.Field.help_text\" title=\"django.forms.Field.help_text\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">help_text</span></code></a> of the field.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.html_name\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">html_name</span></span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.html_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The name that will be used in the widget’s HTML <code class=\"docutils literal notranslate\"><span class=\"pre\">name</span></code> attribute. It takes\nthe form <a class=\"reference internal\" href=\"#django.forms.Form.prefix\" title=\"django.forms.Form.prefix\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">prefix</span></code></a> into account.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.id_for_label\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">id_for_label</span></span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.id_for_label\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Use this property to render the ID of this field. For example, if you are\nmanually constructing a <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code> in your template (despite the fact that\n<a class=\"reference internal\" href=\"#django.forms.BoundField.label_tag\" title=\"django.forms.BoundField.label_tag\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">label_tag()</span></code></a>/<a class=\"reference internal\" href=\"#django.forms.BoundField.legend_tag\" title=\"django.forms.BoundField.legend_tag\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">legend_tag()</span></code></a> will do this\nfor you):</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"p\">&lt;</span><span class=\"nt\">label</span> <span class=\"na\">for</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{{</span> <span class=\"nv\">form.my_field.id_for_label</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>...<span class=\"p\">&lt;/</span><span class=\"nt\">label</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">my_field</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>By default, this will be the field’s name prefixed by <code class=\"docutils literal notranslate\"><span class=\"pre\">id_</span></code>\n(”<code class=\"docutils literal notranslate\"><span class=\"pre\">id_my_field</span></code>” for the example above). You may modify the ID by setting\n<a class=\"reference internal\" href=\"/pl/5.1/ref/forms/widgets/#django.forms.Widget.attrs\" title=\"django.forms.Widget.attrs\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">attrs</span></code></a> on the field’s widget. For example,\ndeclaring a field 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=\"n\">my_field</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\">widget</span><span class=\"o\">=</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">TextInput</span><span class=\"p\">(</span><span class=\"n\">attrs</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;id&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;myFIELD&quot;</span><span class=\"p\">}))</span>\n</code></pre></div>\n<p>and using the template above, would render something like:</p>\n<div class=\"code-block\" data-language=\"html\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Html</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=\"Html code\"><code><span class=\"p\">&lt;</span><span class=\"nt\">label</span> <span class=\"na\">for</span><span class=\"o\">=</span><span class=\"s\">&quot;myFIELD&quot;</span><span class=\"p\">&gt;</span>...<span class=\"p\">&lt;/</span><span class=\"nt\">label</span><span class=\"p\">&gt;&lt;</span><span class=\"nt\">input</span> <span class=\"na\">id</span><span class=\"o\">=</span><span class=\"s\">&quot;myFIELD&quot;</span> <span class=\"na\">type</span><span class=\"o\">=</span><span class=\"s\">&quot;text&quot;</span> <span class=\"na\">name</span><span class=\"o\">=</span><span class=\"s\">&quot;my_field&quot;</span> <span class=\"na\">required</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.initial\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">initial</span></span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.initial\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Use <a class=\"reference internal\" href=\"#django.forms.BoundField.initial\" title=\"django.forms.BoundField.initial\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">BoundField.initial</span></code></a> to retrieve initial data for a form field.\nIt retrieves the data from <a class=\"reference internal\" href=\"#django.forms.Form.initial\" title=\"django.forms.Form.initial\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Form.initial</span></code></a> if present, otherwise\ntrying <a class=\"reference internal\" href=\"/pl/5.1/ref/forms/fields/#django.forms.Field.initial\" title=\"django.forms.Field.initial\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Field.initial</span></code></a>. Callable values are evaluated. See\n<a class=\"reference internal\" href=\"#ref-forms-initial-form-values\"><span class=\"std std-ref\">Initial form values</span></a> for more examples.</p>\n<p><a class=\"reference internal\" href=\"#django.forms.BoundField.initial\" title=\"django.forms.BoundField.initial\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">BoundField.initial</span></code></a> caches its return value, which is useful\nespecially when dealing with callables whose return values can change (e.g.\n<code class=\"docutils literal notranslate\"><span class=\"pre\">datetime.now</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">uuid.uuid4</span></code>):</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">datetime</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">datetime</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">DatedCommentForm</span><span class=\"p\">(</span><span class=\"n\">CommentForm</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"n\">created</span> <span class=\"o\">=</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">DateTimeField</span><span class=\"p\">(</span><span class=\"n\">initial</span><span class=\"o\">=</span><span class=\"n\">datetime</span><span class=\"o\">.</span><span class=\"n\">now</span><span class=\"p\">)</span>\n<span class=\"gp\">...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">DatedCommentForm</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;created&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">initial</span>\n<span class=\"go\">datetime.datetime(2021, 7, 27, 9, 5, 54)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;created&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">initial</span>\n<span class=\"go\">datetime.datetime(2021, 7, 27, 9, 5, 54)</span>\n</code></pre></div>\n<p>Using <a class=\"reference internal\" href=\"#django.forms.BoundField.initial\" title=\"django.forms.BoundField.initial\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">BoundField.initial</span></code></a> is recommended over\n<a class=\"reference internal\" href=\"#django.forms.Form.get_initial_for_field\" title=\"django.forms.Form.get_initial_for_field\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">get_initial_for_field()</span></code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.is_hidden\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">is_hidden</span></span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.is_hidden\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> if this <a class=\"reference internal\" href=\"#django.forms.BoundField\" title=\"django.forms.BoundField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">BoundField</span></code></a>’s widget is\nhidden.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.label\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">label</span></span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.label\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The <a class=\"reference internal\" href=\"/pl/5.1/ref/forms/fields/#django.forms.Field.label\" title=\"django.forms.Field.label\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">label</span></code></a> of the field. This is used in\n<a class=\"reference internal\" href=\"#django.forms.BoundField.label_tag\" title=\"django.forms.BoundField.label_tag\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">label_tag()</span></code></a>/<a class=\"reference internal\" href=\"#django.forms.BoundField.legend_tag\" title=\"django.forms.BoundField.legend_tag\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">legend_tag()</span></code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.name\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">name</span></span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The name of this field in the form:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">name</span><span class=\"p\">)</span>\n<span class=\"go\">subject</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;message&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">name</span><span class=\"p\">)</span>\n<span class=\"go\">message</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.template_name\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">template_name</span></span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.template_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"version-note version-added\" data-version=\"5.0\">\n<p class=\"version-note-title\">New in Django 5.0</p></aside>\n<p>The name of the template rendered with <a class=\"reference internal\" href=\"#django.forms.BoundField.as_field_group\" title=\"django.forms.BoundField.as_field_group\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">BoundField.as_field_group()</span></code></a>.</p>\n<p>A property returning the value of the\n<a class=\"reference internal\" href=\"/pl/5.1/ref/forms/fields/#django.forms.Field.template_name\" title=\"django.forms.Field.template_name\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">template_name</span></code></a> if set otherwise\n<a class=\"reference internal\" href=\"/pl/5.1/ref/forms/renderers/#django.forms.renderers.BaseRenderer.field_template_name\" title=\"django.forms.renderers.BaseRenderer.field_template_name\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">field_template_name</span></code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.use_fieldset\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">use_fieldset</span></span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.use_fieldset\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the value of this BoundField widget’s <code class=\"docutils literal notranslate\"><span class=\"pre\">use_fieldset</span></code> attribute.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.widget_type\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">widget_type</span></span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.widget_type\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the lowercased class name of the wrapped field’s widget, with any\ntrailing <code class=\"docutils literal notranslate\"><span class=\"pre\">input</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">widget</span></code> removed. This may be used when building\nforms where the layout is dependent upon the widget type. For example:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">field</span> <span class=\"k\">in</span> <span class=\"nv\">form</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">field.widget_type</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;checkbox&#39;</span> <span class=\"cp\">%}</span>\n        # render one way\n    <span class=\"cp\">{%</span> <span class=\"k\">else</span> <span class=\"cp\">%}</span>\n        # render another way\n    <span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</dd></dl>\n\n</section>\n<section id=\"methods-of-boundfield\">\n<h3>Methods of <code class=\"docutils literal notranslate\"><span class=\"pre\">BoundField</span></code><a class=\"heading-anchor\" href=\"#methods-of-boundfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.as_field_group\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">as_field_group</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.as_field_group\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"version-note version-added\" data-version=\"5.0\">\n<p class=\"version-note-title\">New in Django 5.0</p></aside>\n<p>Renders the field using <a class=\"reference internal\" href=\"#django.forms.BoundField.render\" title=\"django.forms.BoundField.render\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">BoundField.render()</span></code></a> with default values\nwhich renders the <code class=\"docutils literal notranslate\"><span class=\"pre\">BoundField</span></code>, including its label, help text and errors\nusing the template’s <a class=\"reference internal\" href=\"/pl/5.1/ref/forms/fields/#django.forms.Field.template_name\" title=\"django.forms.Field.template_name\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">template_name</span></code></a> if set\notherwise <a class=\"reference internal\" href=\"/pl/5.1/ref/forms/renderers/#django.forms.renderers.BaseRenderer.field_template_name\" title=\"django.forms.renderers.BaseRenderer.field_template_name\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">field_template_name</span></code></a></p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.as_hidden\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">as_hidden</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">attrs</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"o\"><span class=\"pre\">**</span></span><span class=\"n\"><span class=\"pre\">kwargs</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.as_hidden\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a string of HTML for representing this as an <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input</span> <span class=\"pre\">type=&quot;hidden&quot;&gt;</span></code>.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">**kwargs</span></code> are passed to <a class=\"reference internal\" href=\"#django.forms.BoundField.as_widget\" title=\"django.forms.BoundField.as_widget\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">as_widget()</span></code></a>.</p>\n<p>This method is primarily used internally. You should use a widget instead.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.as_widget\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">as_widget</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">widget</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">attrs</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">only_initial</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.as_widget\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Renders the field by rendering the passed widget, adding any HTML\nattributes passed as <code class=\"docutils literal notranslate\"><span class=\"pre\">attrs</span></code>.  If no widget is specified, then the\nfield’s default widget will be used.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">only_initial</span></code> is used by Django internals and should not be set\nexplicitly.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.css_classes\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">css_classes</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">extra_classes</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.css_classes\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>When you use Django’s rendering shortcuts, CSS classes are used to\nindicate required form fields or fields that contain errors. If you’re\nmanually rendering a form, you can access these CSS classes using the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">css_classes</span></code> method:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;message&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;&quot;</span><span class=\"p\">})</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;message&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">css_classes</span><span class=\"p\">()</span>\n<span class=\"go\">&#39;required&#39;</span>\n</code></pre></div>\n<p>If you want to provide some additional classes in addition to the\nerror and required classes that may be required, you can provide\nthose classes as an argument:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;message&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;&quot;</span><span class=\"p\">})</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;message&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">css_classes</span><span class=\"p\">(</span><span class=\"s2\">&quot;foo bar&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">&#39;foo bar required&#39;</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.get_context\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">get_context</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.get_context\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"version-note version-added\" data-version=\"5.0\">\n<p class=\"version-note-title\">New in Django 5.0</p></aside>\n<p>Return the template context for rendering the field. The available context\nis <code class=\"docutils literal notranslate\"><span class=\"pre\">field</span></code> being the instance of the bound field.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.label_tag\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">label_tag</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">contents</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">attrs</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">label_suffix</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">tag</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.label_tag\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Renders a label tag for the form field using the template specified by\n<a class=\"reference internal\" href=\"#django.forms.Form.template_name_label\" title=\"django.forms.Form.template_name_label\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Form.template_name_label</span></code></a>.</p>\n<p>The available context is:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">field</span></code>: This instance of the <a class=\"reference internal\" href=\"#django.forms.BoundField\" title=\"django.forms.BoundField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">BoundField</span></code></a>.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">contents</span></code>: By default a concatenated string of\n<a class=\"reference internal\" href=\"#django.forms.BoundField.label\" title=\"django.forms.BoundField.label\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">BoundField.label</span></code></a> and <a class=\"reference internal\" href=\"#django.forms.Form.label_suffix\" title=\"django.forms.Form.label_suffix\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Form.label_suffix</span></code></a> (or\n<a class=\"reference internal\" href=\"/pl/5.1/ref/forms/fields/#django.forms.Field.label_suffix\" title=\"django.forms.Field.label_suffix\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Field.label_suffix</span></code></a>, if set). This can be overridden by the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">contents</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">label_suffix</span></code> arguments.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">attrs</span></code>: A <code class=\"docutils literal notranslate\"><span class=\"pre\">dict</span></code> containing <code class=\"docutils literal notranslate\"><span class=\"pre\">for</span></code>,\n<a class=\"reference internal\" href=\"#django.forms.Form.required_css_class\" title=\"django.forms.Form.required_css_class\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Form.required_css_class</span></code></a>, and <code class=\"docutils literal notranslate\"><span class=\"pre\">id</span></code>. <code class=\"docutils literal notranslate\"><span class=\"pre\">id</span></code> is generated by the\nfield’s widget <code class=\"docutils literal notranslate\"><span class=\"pre\">attrs</span></code> or <a class=\"reference internal\" href=\"#django.forms.BoundField.auto_id\" title=\"django.forms.BoundField.auto_id\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">BoundField.auto_id</span></code></a>. Additional\nattributes can be provided by the <code class=\"docutils literal notranslate\"><span class=\"pre\">attrs</span></code> argument.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">use_tag</span></code>: A boolean which is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> if the label has an <code class=\"docutils literal notranslate\"><span class=\"pre\">id</span></code>.\nIf <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code> the default template omits the <code class=\"docutils literal notranslate\"><span class=\"pre\">tag</span></code>.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">tag</span></code>: An optional string to customize the tag, defaults to <code class=\"docutils literal notranslate\"><span class=\"pre\">label</span></code>.</p></li>\n</ul>\n<aside class=\"admonition admonition-tip\">\n<p class=\"admonition-title\">Wskazówka</p>\n<p>In your template <code class=\"docutils literal notranslate\"><span class=\"pre\">field</span></code> is the instance of the <code class=\"docutils literal notranslate\"><span class=\"pre\">BoundField</span></code>.\nTherefore <code class=\"docutils literal notranslate\"><span class=\"pre\">field.field</span></code> accesses <a class=\"reference internal\" href=\"#django.forms.BoundField.field\" title=\"django.forms.BoundField.field\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">BoundField.field</span></code></a> being\nthe field you declare, e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">forms.CharField</span></code>.</p>\n</aside>\n<p>To separately render the label tag of a form field, you can call its\n<code class=\"docutils literal notranslate\"><span class=\"pre\">label_tag()</span></code> method:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;message&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;&quot;</span><span class=\"p\">})</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">[</span><span class=\"s2\">&quot;message&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">label_tag</span><span class=\"p\">())</span>\n<span class=\"go\">&lt;label for=&quot;id_message&quot;&gt;Message:&lt;/label&gt;</span>\n</code></pre></div>\n<p>If you’d like to customize the rendering this can be achieved by overriding\nthe <a class=\"reference internal\" href=\"#django.forms.Form.template_name_label\" title=\"django.forms.Form.template_name_label\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Form.template_name_label</span></code></a> attribute or more generally by\noverriding the default template, see also\n<a class=\"reference internal\" href=\"/pl/5.1/ref/forms/renderers/#overriding-built-in-form-templates\"><span class=\"std std-ref\">Overriding built-in form templates</span></a>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.legend_tag\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">legend_tag</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">contents</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">attrs</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">label_suffix</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.legend_tag\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Calls <a class=\"reference internal\" href=\"#django.forms.BoundField.label_tag\" title=\"django.forms.BoundField.label_tag\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">label_tag()</span></code></a> with <code class=\"docutils literal notranslate\"><span class=\"pre\">tag='legend'</span></code> to render the label with\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;legend&gt;</span></code> tags. This is useful when rendering radio and multiple\ncheckbox widgets where <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;legend&gt;</span></code> may be more appropriate than a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.render\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">render</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">template_name</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">context</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">renderer</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.render\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"version-note version-added\" data-version=\"5.0\">\n<p class=\"version-note-title\">New in Django 5.0</p></aside>\n<p>The render method is called by <code class=\"docutils literal notranslate\"><span class=\"pre\">as_field_group</span></code>. All arguments are\noptional and default to:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">template_name</span></code>: <a class=\"reference internal\" href=\"#django.forms.BoundField.template_name\" title=\"django.forms.BoundField.template_name\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">BoundField.template_name</span></code></a></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">context</span></code>: Value returned by <a class=\"reference internal\" href=\"#django.forms.BoundField.get_context\" title=\"django.forms.BoundField.get_context\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">BoundField.get_context()</span></code></a></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">renderer</span></code>: Value returned by <a class=\"reference internal\" href=\"#django.forms.Form.default_renderer\" title=\"django.forms.Form.default_renderer\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Form.default_renderer</span></code></a></p></li>\n</ul>\n<p>By passing <code class=\"docutils literal notranslate\"><span class=\"pre\">template_name</span></code> you can customize the template used for just a\nsingle call.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.BoundField.value\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BoundField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">value</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.BoundField.value\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Use this method to render the raw value of this field as it would be rendered\nby a <code class=\"docutils literal notranslate\"><span class=\"pre\">Widget</span></code>:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">initial</span> <span class=\"o\">=</span> <span class=\"p\">{</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;welcome&quot;</span><span class=\"p\">}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">unbound_form</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">initial</span><span class=\"o\">=</span><span class=\"n\">initial</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">bound_form</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;hi&quot;</span><span class=\"p\">},</span> <span class=\"n\">initial</span><span class=\"o\">=</span><span class=\"n\">initial</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">unbound_form</span><span class=\"p\">[</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">value</span><span class=\"p\">())</span>\n<span class=\"go\">welcome</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">bound_form</span><span class=\"p\">[</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">value</span><span class=\"p\">())</span>\n<span class=\"go\">hi</span>\n</code></pre></div>\n</dd></dl>\n\n</section>\n</section>\n<section id=\"customizing-boundfield\">\n<h2>Customizing <code class=\"docutils literal notranslate\"><span class=\"pre\">BoundField</span></code><a class=\"heading-anchor\" href=\"#customizing-boundfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>If you need to access some additional information about a form field in a\ntemplate and using a subclass of <a class=\"reference internal\" href=\"/pl/5.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> isn’t\nsufficient, consider also customizing <a class=\"reference internal\" href=\"#django.forms.BoundField\" title=\"django.forms.BoundField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">BoundField</span></code></a>.</p>\n<p>A custom form field can override <code class=\"docutils literal notranslate\"><span class=\"pre\">get_bound_field()</span></code>:</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Field.get_bound_field\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Field.</span></span><span class=\"sig-name descname\"><span class=\"pre\">get_bound_field</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">form</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">field_name</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Field.get_bound_field\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Takes an instance of <a class=\"reference internal\" href=\"#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> and the name of the field.\nThe return value will be used when accessing the field in a template. Most\nlikely it will be an instance of a subclass of\n<a class=\"reference internal\" href=\"#django.forms.BoundField\" title=\"django.forms.BoundField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">BoundField</span></code></a>.</p>\n</dd></dl>\n\n<p>If you have a <code class=\"docutils literal notranslate\"><span class=\"pre\">GPSCoordinatesField</span></code>, for example, and want to be able to\naccess additional information about the coordinates in a template, this could\nbe implemented as follows:</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\">GPSCoordinatesBoundField</span><span class=\"p\">(</span><span class=\"n\">BoundField</span><span class=\"p\">):</span>\n    <span class=\"nd\">@property</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">country</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Return the country the coordinates lie in or None if it can&#39;t be</span>\n<span class=\"sd\">        determined.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">value</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">value</span><span class=\"p\">()</span>\n        <span class=\"k\">if</span> <span class=\"n\">value</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"n\">get_country_from_coordinates</span><span class=\"p\">(</span><span class=\"n\">value</span><span class=\"p\">)</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"kc\">None</span>\n\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">GPSCoordinatesField</span><span class=\"p\">(</span><span class=\"n\">Field</span><span class=\"p\">):</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">get_bound_field</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">form</span><span class=\"p\">,</span> <span class=\"n\">field_name</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"n\">GPSCoordinatesBoundField</span><span class=\"p\">(</span><span class=\"n\">form</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">field_name</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Now you can access the country in a template with\n<code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">form.coordinates.country</span> <span class=\"pre\">}}</span></code>.</p>\n</section>\n<section id=\"binding-uploaded-files-to-a-form\">\n<span id=\"binding-uploaded-files\"></span><h2>Binding uploaded files to a form<a class=\"heading-anchor\" href=\"#binding-uploaded-files-to-a-form\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Dealing with forms that have <code class=\"docutils literal notranslate\"><span class=\"pre\">FileField</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">ImageField</span></code> fields\nis a little more complicated than a normal form.</p>\n<p>Firstly, in order to upload files, you’ll need to make sure that your\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;form&gt;</span></code> element correctly defines the <code class=\"docutils literal notranslate\"><span class=\"pre\">enctype</span></code> as\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;multipart/form-data&quot;</span></code>:</p>\n<div class=\"code-block\" data-language=\"html\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Html</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=\"Html code\"><code><span class=\"p\">&lt;</span><span class=\"nt\">form</span> <span class=\"na\">enctype</span><span class=\"o\">=</span><span class=\"s\">&quot;multipart/form-data&quot;</span> <span class=\"na\">method</span><span class=\"o\">=</span><span class=\"s\">&quot;post&quot;</span> <span class=\"na\">action</span><span class=\"o\">=</span><span class=\"s\">&quot;/foo/&quot;</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>Secondly, when you use the form, you need to bind the file data. File\ndata is handled separately to normal form data, so when your form\ncontains a <code class=\"docutils literal notranslate\"><span class=\"pre\">FileField</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">ImageField</span></code>, you will need to specify\na second argument when you bind your form. So if we extend our\nContactForm to include an <code class=\"docutils literal notranslate\"><span class=\"pre\">ImageField</span></code> called <code class=\"docutils literal notranslate\"><span class=\"pre\">mugshot</span></code>, we\nneed to bind the file data containing the mugshot image:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"go\"># Bound form with an image field</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.files.uploadedfile</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">SimpleUploadedFile</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">data</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;hello&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;message&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Hi there&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;sender&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;foo@example.com&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;cc_myself&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span><span class=\"p\">}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">file_data</span> <span class=\"o\">=</span> <span class=\"p\">{</span><span class=\"s2\">&quot;mugshot&quot;</span><span class=\"p\">:</span> <span class=\"n\">SimpleUploadedFile</span><span class=\"p\">(</span><span class=\"s2\">&quot;face.jpg&quot;</span><span class=\"p\">,</span> <span class=\"sa\">b</span><span class=\"s2\">&quot;file data&quot;</span><span class=\"p\">)}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactFormWithMugshot</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"p\">,</span> <span class=\"n\">file_data</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>In practice, you will usually specify <code class=\"docutils literal notranslate\"><span class=\"pre\">request.FILES</span></code> as the source\nof file data (just like you use <code class=\"docutils literal notranslate\"><span class=\"pre\">request.POST</span></code> as the source of\nform data):</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"go\"># Bound form with an image field, data from the request</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactFormWithMugshot</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">POST</span><span class=\"p\">,</span> <span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">FILES</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Constructing an unbound form is the same as always – omit both form data <em>and</em>\nfile data:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"go\"># Unbound form with an image field</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactFormWithMugshot</span><span class=\"p\">()</span>\n</code></pre></div>\n<section id=\"testing-for-multipart-forms\">\n<h3>Testing for multipart forms<a class=\"heading-anchor\" href=\"#testing-for-multipart-forms\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.is_multipart\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">is_multipart</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Form.is_multipart\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>If you’re writing reusable views or templates, you may not know ahead of time\nwhether your form is a multipart form or not. The <code class=\"docutils literal notranslate\"><span class=\"pre\">is_multipart()</span></code> method\ntells you whether the form requires multipart encoding for submission:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactFormWithMugshot</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">is_multipart</span><span class=\"p\">()</span>\n<span class=\"go\">True</span>\n</code></pre></div>\n<p>Here’s an example of how you might use this in a template:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">form.is_multipart</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">form</span> <span class=\"na\">enctype</span><span class=\"o\">=</span><span class=\"s\">&quot;multipart/form-data&quot;</span> <span class=\"na\">method</span><span class=\"o\">=</span><span class=\"s\">&quot;post&quot;</span> <span class=\"na\">action</span><span class=\"o\">=</span><span class=\"s\">&quot;/foo/&quot;</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">else</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">form</span> <span class=\"na\">method</span><span class=\"o\">=</span><span class=\"s\">&quot;post&quot;</span> <span class=\"na\">action</span><span class=\"o\">=</span><span class=\"s\">&quot;/foo/&quot;</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{{</span> <span class=\"nv\">form</span> <span class=\"cp\">}}</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">form</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"subclassing-forms\">\n<h2>Subclassing forms<a class=\"heading-anchor\" href=\"#subclassing-forms\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>If you have multiple <code class=\"docutils literal notranslate\"><span class=\"pre\">Form</span></code> classes that share fields, you can use\nsubclassing to remove redundancy.</p>\n<p>When you subclass a custom <code class=\"docutils literal notranslate\"><span class=\"pre\">Form</span></code> class, the resulting subclass will\ninclude all fields of the parent class(es), followed by the fields you define\nin the subclass.</p>\n<p>In this example, <code class=\"docutils literal notranslate\"><span class=\"pre\">ContactFormWithPriority</span></code> contains all the fields from\n<code class=\"docutils literal notranslate\"><span class=\"pre\">ContactForm</span></code>, plus an additional field, <code class=\"docutils literal notranslate\"><span class=\"pre\">priority</span></code>. The <code class=\"docutils literal notranslate\"><span class=\"pre\">ContactForm</span></code>\nfields are ordered first:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">ContactFormWithPriority</span><span class=\"p\">(</span><span class=\"n\">ContactForm</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"n\">priority</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=\"gp\">...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactFormWithPriority</span><span class=\"p\">(</span><span class=\"n\">auto_id</span><span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;div&gt;Subject:&lt;input type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; required&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;Message:&lt;textarea name=&quot;message&quot; cols=&quot;40&quot; rows=&quot;10&quot; required&gt;&lt;/textarea&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;Sender:&lt;input type=&quot;email&quot; name=&quot;sender&quot; required&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;Cc myself:&lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot;&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;Priority:&lt;input type=&quot;text&quot; name=&quot;priority&quot; required&gt;&lt;/div&gt;</span>\n</code></pre></div>\n<p>It’s possible to subclass multiple forms, treating forms as mixins. In this\nexample, <code class=\"docutils literal notranslate\"><span class=\"pre\">BeatleForm</span></code> subclasses both <code class=\"docutils literal notranslate\"><span class=\"pre\">PersonForm</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">InstrumentForm</span></code>\n(in that order), and its field list includes the fields from the parent\nclasses:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><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=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">PersonForm</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=\"gp\">... </span>    <span class=\"n\">first_name</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=\"gp\">... </span>    <span class=\"n\">last_name</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=\"gp\">...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">InstrumentForm</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=\"gp\">... </span>    <span class=\"n\">instrument</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=\"gp\">...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">BeatleForm</span><span class=\"p\">(</span><span class=\"n\">InstrumentForm</span><span class=\"p\">,</span> <span class=\"n\">PersonForm</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"n\">haircut_type</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=\"gp\">...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">b</span> <span class=\"o\">=</span> <span class=\"n\">BeatleForm</span><span class=\"p\">(</span><span class=\"n\">auto_id</span><span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">b</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;div&gt;First name:&lt;input type=&quot;text&quot; name=&quot;first_name&quot; required&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;Last name:&lt;input type=&quot;text&quot; name=&quot;last_name&quot; required&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;Instrument:&lt;input type=&quot;text&quot; name=&quot;instrument&quot; required&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;Haircut type:&lt;input type=&quot;text&quot; name=&quot;haircut_type&quot; required&gt;&lt;/div&gt;</span>\n</code></pre></div>\n<p>It’s possible to declaratively remove a <code class=\"docutils literal notranslate\"><span class=\"pre\">Field</span></code> inherited from a parent class\nby setting the name of the field to <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code> on the subclass. For example:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><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=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">ParentForm</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=\"gp\">... </span>    <span class=\"n\">name</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=\"gp\">... </span>    <span class=\"n\">age</span> <span class=\"o\">=</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">IntegerField</span><span class=\"p\">()</span>\n<span class=\"gp\">...</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">ChildForm</span><span class=\"p\">(</span><span class=\"n\">ParentForm</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"n\">name</span> <span class=\"o\">=</span> <span class=\"kc\">None</span>\n<span class=\"gp\">...</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">list</span><span class=\"p\">(</span><span class=\"n\">ChildForm</span><span class=\"p\">()</span><span class=\"o\">.</span><span class=\"n\">fields</span><span class=\"p\">)</span>\n<span class=\"go\">[&#39;age&#39;]</span>\n</code></pre></div>\n</section>\n<section id=\"prefixes-for-forms\">\n<span id=\"form-prefix\"></span><h2>Prefixes for forms<a class=\"heading-anchor\" href=\"#prefixes-for-forms\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Form.prefix\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Form.</span></span><span class=\"sig-name descname\"><span class=\"pre\">prefix</span></span><a class=\"heading-anchor\" href=\"#django.forms.Form.prefix\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>You can put several Django forms inside one <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;form&gt;</span></code> tag. To give each\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Form</span></code> its own namespace, use the <code class=\"docutils literal notranslate\"><span class=\"pre\">prefix</span></code> keyword argument:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">mother</span> <span class=\"o\">=</span> <span class=\"n\">PersonForm</span><span class=\"p\">(</span><span class=\"n\">prefix</span><span class=\"o\">=</span><span class=\"s2\">&quot;mother&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">father</span> <span class=\"o\">=</span> <span class=\"n\">PersonForm</span><span class=\"p\">(</span><span class=\"n\">prefix</span><span class=\"o\">=</span><span class=\"s2\">&quot;father&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">mother</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_mother-first_name&quot;&gt;First name:&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;mother-first_name&quot; required id=&quot;id_mother-first_name&quot;&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_mother-last_name&quot;&gt;Last name:&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;mother-last_name&quot; required id=&quot;id_mother-last_name&quot;&gt;&lt;/div&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">father</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_father-first_name&quot;&gt;First name:&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;father-first_name&quot; required id=&quot;id_father-first_name&quot;&gt;&lt;/div&gt;</span>\n<span class=\"go\">&lt;div&gt;&lt;label for=&quot;id_father-last_name&quot;&gt;Last name:&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;father-last_name&quot; required id=&quot;id_father-last_name&quot;&gt;&lt;/div&gt;</span>\n</code></pre></div>\n<p>The prefix can also be specified on the form class:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">PersonForm</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=\"gp\">... </span>    <span class=\"o\">...</span>\n<span class=\"gp\">... </span>    <span class=\"n\">prefix</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;person&quot;</span>\n<span class=\"gp\">...</span>\n</code></pre></div>\n</section>","rootId":"module-django.forms","toc":[{"title":"Bound and unbound forms","anchor":"bound-and-unbound-forms","children":[]},{"title":"Using forms to validate data","anchor":"using-forms-to-validate-data","children":[{"title":"Behavior of unbound forms","anchor":"behavior-of-unbound-forms","children":[]}]},{"title":"Initial form values","anchor":"initial-form-values","children":[]},{"title":"Checking which form data has changed","anchor":"checking-which-form-data-has-changed","children":[]},{"title":"Accessing the fields from the form","anchor":"accessing-the-fields-from-the-form","children":[]},{"title":"Accessing „clean” data","anchor":"accessing-clean-data","children":[]},{"title":"Outputting forms as HTML","anchor":"outputting-forms-as-html","children":[{"title":"Default rendering","anchor":"default-rendering","children":[{"title":"template_name","anchor":"template-name","children":[]},{"title":"render()","anchor":"render","children":[]},{"title":"get_context()","anchor":"get-context","children":[]},{"title":"template_name_label","anchor":"template-name-label","children":[]}]},{"title":"Output styles","anchor":"output-styles","children":[{"title":"as_div()","anchor":"as-div","children":[]},{"title":"as_p()","anchor":"as-p","children":[]},{"title":"as_ul()","anchor":"as-ul","children":[]},{"title":"as_table()","anchor":"as-table","children":[]}]},{"title":"Styling required or erroneous form rows","anchor":"styling-required-or-erroneous-form-rows","children":[]},{"title":"Configuring form elements» HTML id attributes and <label> tags","anchor":"configuring-form-elements-html-id-attributes-and-label-tags","children":[]},{"title":"Configuring the rendering of a form’s widgets","anchor":"configuring-the-rendering-of-a-form-s-widgets","children":[]},{"title":"Notes on field ordering","anchor":"notes-on-field-ordering","children":[]},{"title":"How errors are displayed","anchor":"how-errors-are-displayed","children":[]},{"title":"Customizing the error list format","anchor":"customizing-the-error-list-format","children":[]}]},{"title":"More granular output","anchor":"more-granular-output","children":[{"title":"Attributes of BoundField","anchor":"attributes-of-boundfield","children":[]},{"title":"Methods of BoundField","anchor":"methods-of-boundfield","children":[]}]},{"title":"Customizing BoundField","anchor":"customizing-boundfield","children":[]},{"title":"Binding uploaded files to a form","anchor":"binding-uploaded-files-to-a-form","children":[{"title":"Testing for multipart forms","anchor":"testing-for-multipart-forms","children":[]}]},{"title":"Subclassing forms","anchor":"subclassing-forms","children":[]},{"title":"Prefixes for forms","anchor":"prefixes-for-forms","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/pl/5.1/ref/"},{"docname":"ref/forms/index","title":"Forms","url":"/pl/5.1/ref/forms/"}],"prev":{"docname":"ref/forms/index","title":"Forms","url":"/pl/5.1/ref/forms/"},"next":{"docname":"ref/forms/fields","title":"Form fields","url":"/pl/5.1/ref/forms/fields/"},"formats":{"html":"/pl/5.1/ref/forms/api/","markdown":"/pl/5.1/ref/forms/api.md","json":"/pl/5.1/ref/forms/api.json"},"source":"https://github.com/django/django/blob/stable/5.1.x/docs/ref/forms/api.txt","official":"https://docs.djangoproject.com/pl/5.1/ref/forms/api/","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","1.11","1.10"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}