{"title":"Working with forms","version":"4.1","locale":"es","docname":"topics/forms/index","url":"/es/4.1/topics/forms/","canonical":"https://djangodocs.dev/es/4.1/topics/forms/","summary":"About this document This document provides an introduction to the basics of web forms and how they are handled in Django. For a more detailed look at specific areas…","html":"<h1>Working with forms<a class=\"heading-anchor\" href=\"#working-with-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 provides an introduction to the basics of web forms and how\nthey are handled in Django. For a more detailed look at specific areas of\nthe forms API, see <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/\"><span class=\"doc\">The Forms API</span></a>, <a class=\"reference internal\" href=\"/es/4.1/ref/forms/fields/\"><span class=\"doc\">Form fields</span></a>, and\n<a class=\"reference internal\" href=\"/es/4.1/ref/forms/validation/\"><span class=\"doc\">Form and field validation</span></a>.</p>\n</aside>\n<p>Unless you’re planning to build websites and applications that do nothing but\npublish content, and don’t accept input from your visitors, you’re going to\nneed to understand and use forms.</p>\n<p>Django provides a range of tools and libraries to help you build forms to\naccept input from site visitors, and then process and respond to the input.</p>\n<section id=\"html-forms\">\n<h2>HTML forms<a class=\"heading-anchor\" href=\"#html-forms\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>In HTML, a form is a collection of elements inside <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;form&gt;...&lt;/form&gt;</span></code> that\nallow a visitor to do things like enter text, select options, manipulate\nobjects or controls, and so on, and then send that information back to the\nserver.</p>\n<p>Some of these form interface elements - text input or checkboxes - are built\ninto HTML itself. Others are much more complex; an interface that pops up a\ndate picker or allows you to move a slider or manipulate controls will\ntypically use JavaScript and CSS as well as HTML form <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input&gt;</span></code> elements to\nachieve these effects.</p>\n<p>As well as its <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input&gt;</span></code> elements, a form must specify two things:</p>\n<ul class=\"simple\">\n<li><p><em>where</em>: the URL to which the data corresponding to the user’s input should\nbe returned</p></li>\n<li><p><em>how</em>: the HTTP method the data should be returned by</p></li>\n</ul>\n<p>As an example, the login form for the Django admin contains several\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input&gt;</span></code> elements: one of <code class=\"docutils literal notranslate\"><span class=\"pre\">type=&quot;text&quot;</span></code> for the username, one of\n<code class=\"docutils literal notranslate\"><span class=\"pre\">type=&quot;password&quot;</span></code> for the password, and one of <code class=\"docutils literal notranslate\"><span class=\"pre\">type=&quot;submit&quot;</span></code> for the\n«Log in» button. It also contains some hidden text fields that the user\ndoesn’t see, which Django uses to determine what to do next.</p>\n<p>It also tells the browser that the form data should be sent to the URL\nspecified in the <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;form&gt;</span></code>’s <code class=\"docutils literal notranslate\"><span class=\"pre\">action</span></code> attribute - <code class=\"docutils literal notranslate\"><span class=\"pre\">/admin/</span></code> - and that it\nshould be sent using the HTTP mechanism specified by the <code class=\"docutils literal notranslate\"><span class=\"pre\">method</span></code> attribute -\n<code class=\"docutils literal notranslate\"><span class=\"pre\">post</span></code>.</p>\n<p>When the <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input</span> <span class=\"pre\">type=&quot;submit&quot;</span> <span class=\"pre\">value=&quot;Log</span> <span class=\"pre\">in&quot;&gt;</span></code> element is triggered, the\ndata is returned to <code class=\"docutils literal notranslate\"><span class=\"pre\">/admin/</span></code>.</p>\n<section id=\"get-and-post\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">GET</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">POST</span></code><a class=\"heading-anchor\" href=\"#get-and-post\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">GET</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">POST</span></code> are the only HTTP methods to use when dealing with forms.</p>\n<p>Django’s login form is returned using the <code class=\"docutils literal notranslate\"><span class=\"pre\">POST</span></code> method, in which the browser\nbundles up the form data, encodes it for transmission, sends it to the server,\nand then receives back its response.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">GET</span></code>, by contrast, bundles the submitted data into a string, and uses this\nto compose a URL. The URL contains the address where the data must be sent, as\nwell as the data keys and values. You can see this in action if you do a search\nin the Django documentation, which will produce a URL of the form\n<code class=\"docutils literal notranslate\"><span class=\"pre\">https://docs.djangoproject.com/search/?q=forms&amp;release=1</span></code>.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">GET</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">POST</span></code> are typically used for different purposes.</p>\n<p>Any request that could be used to change the state of the system - for example,\na request that makes changes in the database - should use <code class=\"docutils literal notranslate\"><span class=\"pre\">POST</span></code>. <code class=\"docutils literal notranslate\"><span class=\"pre\">GET</span></code>\nshould be used only for requests that do not affect the state of the system.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">GET</span></code> would also be unsuitable for a password form, because the password\nwould appear in the URL, and thus, also in browser history and server logs,\nall in plain text. Neither would it be suitable for large quantities of data,\nor for binary data, such as an image. A web application that uses <code class=\"docutils literal notranslate\"><span class=\"pre\">GET</span></code>\nrequests for admin forms is a security risk: it can be easy for an attacker to\nmimic a form’s request to gain access to sensitive parts of the system.\n<code class=\"docutils literal notranslate\"><span class=\"pre\">POST</span></code>, coupled with other protections like Django’s <a class=\"reference internal\" href=\"/es/4.1/ref/csrf/\"><span class=\"doc\">CSRF protection</span></a> offers more control over access.</p>\n<p>On the other hand, <code class=\"docutils literal notranslate\"><span class=\"pre\">GET</span></code> is suitable for things like a web search form,\nbecause the URLs that represent a <code class=\"docutils literal notranslate\"><span class=\"pre\">GET</span></code> request can easily be bookmarked,\nshared, or resubmitted.</p>\n</section>\n</section>\n<section id=\"django-s-role-in-forms\">\n<h2>Django’s role in forms<a class=\"heading-anchor\" href=\"#django-s-role-in-forms\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Handling forms is a complex business. Consider Django’s admin, where numerous\nitems of data of several different types may need to be prepared for display in\na form, rendered as HTML, edited using a convenient interface, returned to the\nserver, validated and cleaned up, and then saved or passed on for further\nprocessing.</p>\n<p>Django’s form functionality can simplify and automate vast portions of this\nwork, and can also do it more securely than most programmers would be able to\ndo in code they wrote themselves.</p>\n<p>Django handles three distinct parts of the work involved in forms:</p>\n<ul class=\"simple\">\n<li><p>preparing and restructuring data to make it ready for rendering</p></li>\n<li><p>creating HTML forms for the data</p></li>\n<li><p>receiving and processing submitted forms and data from the client</p></li>\n</ul>\n<p>It is <em>possible</em> to write code that does all of this manually, but Django can\ntake care of it all for you.</p>\n</section>\n<section id=\"forms-in-django\">\n<h2>Forms in Django<a class=\"heading-anchor\" href=\"#forms-in-django\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>We’ve described HTML forms briefly, but an HTML <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;form&gt;</span></code> is just one part of\nthe machinery required.</p>\n<p>In the context of a web application, “form” might refer to that HTML\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;form&gt;</span></code>, or to the Django <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> that produces it, or to the\nstructured data returned when it is submitted, or to the end-to-end working\ncollection of these parts.</p>\n<section id=\"the-django-form-class\">\n<h3>The Django <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> class<a class=\"heading-anchor\" href=\"#the-django-form-class\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>At the heart of this system of components is Django’s <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> class. In\nmuch the same way that a Django model describes the logical structure of an\nobject, its behavior, and the way its parts are represented to us, a\n<a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> class describes a form and determines how it works and appears.</p>\n<p>In a similar way that a model class’s fields map to database fields, a form\nclass’s fields map to HTML form <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input&gt;</span></code> elements. (A <a class=\"reference internal\" href=\"/es/4.1/topics/forms/modelforms/#django.forms.ModelForm\" title=\"django.forms.ModelForm\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ModelForm</span></code></a>\nmaps a model class’s fields to HTML form <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input&gt;</span></code> elements via a\n<a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a>; this is what the Django admin is based upon.)</p>\n<p>A form’s fields are themselves classes; they manage form data and perform\nvalidation when a form is submitted. A <a class=\"reference internal\" href=\"/es/4.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> and a\n<a class=\"reference internal\" href=\"/es/4.1/ref/forms/fields/#django.forms.FileField\" title=\"django.forms.FileField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">FileField</span></code></a> handle very different kinds of data and have to do\ndifferent things with it.</p>\n<p>A form field is represented to a user in the browser as an HTML «widget» - a\npiece of user interface machinery. Each field type has an appropriate default\n<a class=\"reference internal\" href=\"/es/4.1/ref/forms/widgets/\"><span class=\"doc\">Widget class</span></a>, but these can be overridden as\nrequired.</p>\n</section>\n<section id=\"instantiating-processing-and-rendering-forms\">\n<h3>Instantiating, processing, and rendering forms<a class=\"heading-anchor\" href=\"#instantiating-processing-and-rendering-forms\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>When rendering an object in Django, we generally:</p>\n<ol class=\"arabic simple\">\n<li><p>get hold of it in the view (fetch it from the database, for example)</p></li>\n<li><p>pass it to the template context</p></li>\n<li><p>expand it to HTML markup using template variables</p></li>\n</ol>\n<p>Rendering a form in a template involves nearly the same work as rendering any\nother kind of object, but there are some key differences.</p>\n<p>In the case of a model instance that contained no data, it would rarely if ever\nbe useful to do anything with it in a template. On the other hand, it makes\nperfect sense to render an unpopulated form - that’s what we do when we want\nthe user to populate it.</p>\n<p>So when we handle a model instance in a view, we typically retrieve it from the\ndatabase. When we’re dealing with a form we typically instantiate it in the\nview.</p>\n<p>When we instantiate a form, we can opt to leave it empty or prepopulate it, for\nexample with:</p>\n<ul class=\"simple\">\n<li><p>data from a saved model instance (as in the case of admin forms for editing)</p></li>\n<li><p>data that we have collated from other sources</p></li>\n<li><p>data received from a previous HTML form submission</p></li>\n</ul>\n<p>The last of these cases is the most interesting, because it’s what makes it\npossible for users not just to read a website, but to send information back\nto it too.</p>\n</section>\n</section>\n<section id=\"building-a-form\">\n<h2>Building a form<a class=\"heading-anchor\" href=\"#building-a-form\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"the-work-that-needs-to-be-done\">\n<h3>The work that needs to be done<a class=\"heading-anchor\" href=\"#the-work-that-needs-to-be-done\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Suppose you want to create a simple form on your website, in order to obtain\nthe user’s name. You’d need something like this in your 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=\"p\">&lt;</span><span class=\"nt\">form</span> <span class=\"na\">action</span><span class=\"o\">=</span><span class=\"s\">&quot;/your-name/&quot;</span> <span class=\"na\">method</span><span class=\"o\">=</span><span class=\"s\">&quot;post&quot;</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;your_name&quot;</span><span class=\"p\">&gt;</span>Your name: <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\">id</span><span class=\"o\">=</span><span class=\"s\">&quot;your_name&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;your_name&quot;</span> <span class=\"na\">value</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{{</span> <span class=\"nv\">current_name</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</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;submit&quot;</span> <span class=\"na\">value</span><span class=\"o\">=</span><span class=\"s\">&quot;OK&quot;</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">form</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>This tells the browser to return the form data to the URL <code class=\"docutils literal notranslate\"><span class=\"pre\">/your-name/</span></code>, using\nthe <code class=\"docutils literal notranslate\"><span class=\"pre\">POST</span></code> method. It will display a text field, labeled «Your name:», and a\nbutton marked «OK». If the template context contains a <code class=\"docutils literal notranslate\"><span class=\"pre\">current_name</span></code>\nvariable, that will be used to pre-fill the <code class=\"docutils literal notranslate\"><span class=\"pre\">your_name</span></code> field.</p>\n<p>You’ll need a view that renders the template containing the HTML form, and\nthat can supply the <code class=\"docutils literal notranslate\"><span class=\"pre\">current_name</span></code> field as appropriate.</p>\n<p>When the form is submitted, the <code class=\"docutils literal notranslate\"><span class=\"pre\">POST</span></code> request which is sent to the server\nwill contain the form data.</p>\n<p>Now you’ll also need a view corresponding to that <code class=\"docutils literal notranslate\"><span class=\"pre\">/your-name/</span></code> URL which will\nfind the appropriate key/value pairs in the request, and then process them.</p>\n<p>This is a very simple form. In practice, a form might contain dozens or\nhundreds of fields, many of which might need to be prepopulated, and we might\nexpect the user to work through the edit-submit cycle several times before\nconcluding the operation.</p>\n<p>We might require some validation to occur in the browser, even before the form\nis submitted; we might want to use much more complex fields, that allow the\nuser to do things like pick dates from a calendar and so on.</p>\n<p>At this point it’s much easier to get Django to do most of this work for us.</p>\n</section>\n<section id=\"building-a-form-in-django\">\n<h3>Building a form in Django<a class=\"heading-anchor\" href=\"#building-a-form-in-django\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<section id=\"the-form-class\">\n<h4>The <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> class<a class=\"heading-anchor\" href=\"#the-form-class\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>We already know what we want our HTML form to look like. Our starting point for\nit in Django is this:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">forms.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</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 code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">forms</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">NameForm</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\">your_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\">label</span><span class=\"o\">=</span><span class=\"s1\">&#39;Your name&#39;</span><span class=\"p\">,</span> <span class=\"n\">max_length</span><span class=\"o\">=</span><span class=\"mi\">100</span><span class=\"p\">)</span>\n</code></pre></figure>\n<p>This defines a <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> class with a single field (<code class=\"docutils literal notranslate\"><span class=\"pre\">your_name</span></code>). We’ve\napplied a human-friendly label to the field, which will appear in the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code> when it’s rendered (although in this case, the <a class=\"reference internal\" href=\"/es/4.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>\nwe specified is actually the same one that would be generated automatically if\nwe had omitted it).</p>\n<p>The field’s maximum allowable length is defined by\n<a class=\"reference internal\" href=\"/es/4.1/ref/forms/fields/#django.forms.CharField.max_length\" title=\"django.forms.CharField.max_length\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">max_length</span></code></a>. This does two things. It puts a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">maxlength=&quot;100&quot;</span></code> on the HTML <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input&gt;</span></code> (so the browser should prevent the\nuser from entering more than that number of characters in the first place). It\nalso means that when Django receives the form back from the browser, it will\nvalidate the length of the data.</p>\n<p>A <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#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 an <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#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, which runs\nvalidation routines for all its fields. When this method is called, if all\nfields contain valid data, it will:</p>\n<ul class=\"simple\">\n<li><p>return <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code></p></li>\n<li><p>place the form’s data in its <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#django.forms.Form.cleaned_data\" title=\"django.forms.Form.cleaned_data\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code></a> attribute.</p></li>\n</ul>\n<p>The whole form, when rendered for the first time, will look like:</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;your_name&quot;</span><span class=\"p\">&gt;</span>Your name: <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\">id</span><span class=\"o\">=</span><span class=\"s\">&quot;your_name&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;your_name&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=\"p\">&gt;</span>\n</code></pre></div>\n<p>Note that it <strong>does not</strong> include the <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;form&gt;</span></code> tags, or a submit button.\nWe’ll have to provide those ourselves in the template.</p>\n</section>\n<section id=\"the-view\">\n<span id=\"using-a-form-in-a-view\"></span><h4>The view<a class=\"heading-anchor\" href=\"#the-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Form data sent back to a Django website is processed by a view, generally the\nsame view which published the form. This allows us to reuse some of the same\nlogic.</p>\n<p>To handle the form we need to instantiate it in the view for the URL where we\nwant it to be published:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">views.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</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 code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.http</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">HttpResponseRedirect</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.shortcuts</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">render</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.forms</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">NameForm</span>\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">get_name</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"c1\"># if this is a POST request we need to process the form data</span>\n    <span class=\"k\">if</span> <span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">method</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;POST&#39;</span><span class=\"p\">:</span>\n        <span class=\"c1\"># create a form instance and populate it with data from the request:</span>\n        <span class=\"n\">form</span> <span class=\"o\">=</span> <span class=\"n\">NameForm</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">POST</span><span class=\"p\">)</span>\n        <span class=\"c1\"># check whether it&#39;s valid:</span>\n        <span class=\"k\">if</span> <span class=\"n\">form</span><span class=\"o\">.</span><span class=\"n\">is_valid</span><span class=\"p\">():</span>\n            <span class=\"c1\"># process the data in form.cleaned_data as required</span>\n            <span class=\"c1\"># ...</span>\n            <span class=\"c1\"># redirect to a new URL:</span>\n            <span class=\"k\">return</span> <span class=\"n\">HttpResponseRedirect</span><span class=\"p\">(</span><span class=\"s1\">&#39;/thanks/&#39;</span><span class=\"p\">)</span>\n\n    <span class=\"c1\"># if a GET (or any other method) we&#39;ll create a blank form</span>\n    <span class=\"k\">else</span><span class=\"p\">:</span>\n        <span class=\"n\">form</span> <span class=\"o\">=</span> <span class=\"n\">NameForm</span><span class=\"p\">()</span>\n\n    <span class=\"k\">return</span> <span class=\"n\">render</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">,</span> <span class=\"s1\">&#39;name.html&#39;</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s1\">&#39;form&#39;</span><span class=\"p\">:</span> <span class=\"n\">form</span><span class=\"p\">})</span>\n</code></pre></figure>\n<p>If we arrive at this view with a <code class=\"docutils literal notranslate\"><span class=\"pre\">GET</span></code> request, it will create an empty form\ninstance and place it in the template context to be rendered. This is what we\ncan expect to happen the first time we visit the URL.</p>\n<p>If the form is submitted using a <code class=\"docutils literal notranslate\"><span class=\"pre\">POST</span></code> request, the view will once again\ncreate a form instance and populate it with data from the request: <code class=\"docutils literal notranslate\"><span class=\"pre\">form</span> <span class=\"pre\">=</span>\n<span class=\"pre\">NameForm(request.POST)</span></code> This is called «binding data to the form» (it is now\na <em>bound</em> form).</p>\n<p>We call the form’s <code class=\"docutils literal notranslate\"><span class=\"pre\">is_valid()</span></code> method; if it’s not <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, we go back to\nthe template with the form. This time the form is no longer empty (<em>unbound</em>)\nso the HTML form will be populated with the data previously submitted, where it\ncan be edited and corrected as required.</p>\n<p>If <code class=\"docutils literal notranslate\"><span class=\"pre\">is_valid()</span></code> is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, we’ll now be able to find all the validated form\ndata in its <code class=\"docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code> attribute. We can use this data to update the\ndatabase or do other processing before sending an HTTP redirect to the browser\ntelling it where to go next.</p>\n</section>\n<section id=\"the-template\">\n<span id=\"topics-forms-index-basic-form-template\"></span><h4>The template<a class=\"heading-anchor\" href=\"#the-template\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>We don’t need to do much in our <code class=\"docutils literal notranslate\"><span class=\"pre\">name.html</span></code> 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=\"p\">&lt;</span><span class=\"nt\">form</span> <span class=\"na\">action</span><span class=\"o\">=</span><span class=\"s\">&quot;/your-name/&quot;</span> <span class=\"na\">method</span><span class=\"o\">=</span><span class=\"s\">&quot;post&quot;</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">csrf_token</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\">input</span> <span class=\"na\">type</span><span class=\"o\">=</span><span class=\"s\">&quot;submit&quot;</span> <span class=\"na\">value</span><span class=\"o\">=</span><span class=\"s\">&quot;Submit&quot;</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">form</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>All the form’s fields and their attributes will be unpacked into HTML markup\nfrom that <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">form</span> <span class=\"pre\">}}</span></code> by Django’s template language.</p>\n<aside class=\"admonition-forms-and-cross-site-request-forgery-protection admonition\">\n<p class=\"admonition-title\">Forms and Cross Site Request Forgery protection</p>\n<p>Django ships with an easy-to-use <a class=\"reference internal\" href=\"/es/4.1/ref/csrf/\"><span class=\"doc\">protection against Cross Site Request\nForgeries</span></a>. When submitting a form via <code class=\"docutils literal notranslate\"><span class=\"pre\">POST</span></code> with\nCSRF protection enabled you must use the <a class=\"reference internal\" href=\"/es/4.1/ref/templates/builtins/#std-templatetag-csrf_token\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">csrf_token</span></code></a> template tag\nas in the preceding example. However, since CSRF protection is not\ndirectly tied to forms in templates, this tag is omitted from the\nfollowing examples in this document.</p>\n</aside>\n<aside class=\"admonition-html5-input-types-and-browser-validation admonition\">\n<p class=\"admonition-title\">HTML5 input types and browser validation</p>\n<p>If your form includes a <a class=\"reference internal\" href=\"/es/4.1/ref/forms/fields/#django.forms.URLField\" title=\"django.forms.URLField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">URLField</span></code></a>, an\n<a class=\"reference internal\" href=\"/es/4.1/ref/forms/fields/#django.forms.EmailField\" title=\"django.forms.EmailField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailField</span></code></a> or any integer field type, Django will\nuse the <code class=\"docutils literal notranslate\"><span class=\"pre\">url</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">email</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">number</span></code> HTML5 input types. By default,\nbrowsers may apply their own validation on these fields, which may be\nstricter than Django’s validation. If you would like to disable this\nbehavior, set the <code class=\"docutils literal notranslate\"><span class=\"pre\">novalidate</span></code> attribute on the <code class=\"docutils literal notranslate\"><span class=\"pre\">form</span></code> tag, or specify\na different widget on the field, like <a class=\"reference internal\" href=\"/es/4.1/ref/forms/widgets/#django.forms.TextInput\" title=\"django.forms.TextInput\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">TextInput</span></code></a>.</p>\n</aside>\n<p>We now have a working web form, described by a Django <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a>, processed\nby a view, and rendered as an HTML <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;form&gt;</span></code>.</p>\n<p>That’s all you need to get started, but the forms framework puts a lot more at\nyour fingertips. Once you understand the basics of the process described above,\nyou should be prepared to understand other features of the forms system and\nready to learn a bit more about the underlying machinery.</p>\n</section>\n</section>\n</section>\n<section id=\"more-about-django-form-classes\">\n<h2>More about Django <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Form</span></code></a> classes<a class=\"heading-anchor\" href=\"#more-about-django-form-classes\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>All form classes are created as subclasses of either <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.forms.Form</span></code></a>\nor <a class=\"reference internal\" href=\"/es/4.1/topics/forms/modelforms/#django.forms.ModelForm\" title=\"django.forms.ModelForm\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.forms.ModelForm</span></code></a>. You can think of <code class=\"docutils literal notranslate\"><span class=\"pre\">ModelForm</span></code> as a\nsubclass of <code class=\"docutils literal notranslate\"><span class=\"pre\">Form</span></code>. <code class=\"docutils literal notranslate\"><span class=\"pre\">Form</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">ModelForm</span></code> actually inherit common\nfunctionality from a (private) <code class=\"docutils literal notranslate\"><span class=\"pre\">BaseForm</span></code> class, but this implementation\ndetail is rarely important.</p>\n<aside class=\"admonition-models-and-forms admonition\">\n<p class=\"admonition-title\">Models and Forms</p>\n<p>In fact if your form is going to be used to directly add or edit a Django\nmodel, a <a class=\"reference internal\" href=\"/es/4.1/topics/forms/modelforms/\"><span class=\"doc\">ModelForm</span></a> can save you a great\ndeal of time, effort, and code, because it will build a form, along with the\nappropriate fields and their attributes, from a <code class=\"docutils literal notranslate\"><span class=\"pre\">Model</span></code> class.</p>\n</aside>\n<section id=\"bound-and-unbound-form-instances\">\n<h3>Bound and unbound form instances<a class=\"heading-anchor\" href=\"#bound-and-unbound-form-instances\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The distinction between <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#ref-forms-api-bound-unbound\"><span class=\"std std-ref\">Bound and unbound forms</span></a> is important:</p>\n<ul class=\"simple\">\n<li><p>An unbound form has no data associated with it. When rendered to the user,\nit will be empty or will contain default values.</p></li>\n<li><p>A bound form has submitted data, and hence can be used to tell if that data\nis valid. If an invalid bound form is rendered, it can include inline error\nmessages telling the user what data to correct.</p></li>\n</ul>\n<p>The form’s <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#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 will tell you whether a form has\ndata bound to it or not.</p>\n</section>\n<section id=\"more-on-fields\">\n<h3>More on fields<a class=\"heading-anchor\" href=\"#more-on-fields\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Consider a more useful form than our minimal example above, which we could use\nto implement «contact me» functionality on a personal website:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">forms.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</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 code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">forms</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">Form</span><span class=\"p\">):</span>\n    <span class=\"n\">subject</span> <span class=\"o\">=</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">CharField</span><span class=\"p\">(</span><span class=\"n\">max_length</span><span class=\"o\">=</span><span class=\"mi\">100</span><span class=\"p\">)</span>\n    <span class=\"n\">message</span> <span class=\"o\">=</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">CharField</span><span class=\"p\">(</span><span class=\"n\">widget</span><span class=\"o\">=</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">Textarea</span><span class=\"p\">)</span>\n    <span class=\"n\">sender</span> <span class=\"o\">=</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">EmailField</span><span class=\"p\">()</span>\n    <span class=\"n\">cc_myself</span> <span class=\"o\">=</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">BooleanField</span><span class=\"p\">(</span><span class=\"n\">required</span><span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">)</span>\n</code></pre></figure>\n<p>Our earlier form used a single field, <code class=\"docutils literal notranslate\"><span class=\"pre\">your_name</span></code>, a <a class=\"reference internal\" href=\"/es/4.1/ref/forms/fields/#django.forms.CharField\" title=\"django.forms.CharField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CharField</span></code></a>. In\nthis case, our form has four fields: <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> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">cc_myself</span></code>. <a class=\"reference internal\" href=\"/es/4.1/ref/forms/fields/#django.forms.CharField\" title=\"django.forms.CharField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CharField</span></code></a>, <a class=\"reference internal\" href=\"/es/4.1/ref/forms/fields/#django.forms.EmailField\" title=\"django.forms.EmailField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailField</span></code></a> and\n<a class=\"reference internal\" href=\"/es/4.1/ref/forms/fields/#django.forms.BooleanField\" title=\"django.forms.BooleanField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">BooleanField</span></code></a> are just three of the available field types; a full list\ncan be found in <a class=\"reference internal\" href=\"/es/4.1/ref/forms/fields/\"><span class=\"doc\">Form fields</span></a>.</p>\n<section id=\"widgets\">\n<h4>Widgets<a class=\"heading-anchor\" href=\"#widgets\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Each form field has a corresponding <a class=\"reference internal\" href=\"/es/4.1/ref/forms/widgets/\"><span class=\"doc\">Widget class</span></a>,\nwhich in turn corresponds to an HTML form widget such as <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input</span>\n<span class=\"pre\">type=&quot;text&quot;&gt;</span></code>.</p>\n<p>In most cases, the field will have a sensible default widget. For example, by\ndefault, a <a class=\"reference internal\" href=\"/es/4.1/ref/forms/fields/#django.forms.CharField\" title=\"django.forms.CharField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CharField</span></code></a> will have a <a class=\"reference internal\" href=\"/es/4.1/ref/forms/widgets/#django.forms.TextInput\" title=\"django.forms.TextInput\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">TextInput</span></code></a> widget, that\nproduces an <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input</span> <span class=\"pre\">type=&quot;text&quot;&gt;</span></code> in the HTML. If you needed <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;textarea&gt;</span></code>\ninstead, you’d specify the appropriate widget when defining your form field,\nas we have done for the <code class=\"docutils literal notranslate\"><span class=\"pre\">message</span></code> field.</p>\n</section>\n<section id=\"field-data\">\n<h4>Field data<a class=\"heading-anchor\" href=\"#field-data\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Whatever the data submitted with a form, once it has been successfully\nvalidated by calling <code class=\"docutils literal notranslate\"><span class=\"pre\">is_valid()</span></code> (and <code class=\"docutils literal notranslate\"><span class=\"pre\">is_valid()</span></code> has returned <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>),\nthe validated form data will be in the <code class=\"docutils literal notranslate\"><span class=\"pre\">form.cleaned_data</span></code> dictionary. This\ndata will have been nicely converted into Python types for you.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>You can still access the unvalidated data directly from <code class=\"docutils literal notranslate\"><span class=\"pre\">request.POST</span></code> at\nthis point, but the validated data is better.</p>\n</aside>\n<p>In the contact form example above, <code class=\"docutils literal notranslate\"><span class=\"pre\">cc_myself</span></code> will be a boolean value.\nLikewise, fields such as <a class=\"reference internal\" href=\"/es/4.1/ref/forms/fields/#django.forms.IntegerField\" title=\"django.forms.IntegerField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">IntegerField</span></code></a> and <a class=\"reference internal\" href=\"/es/4.1/ref/forms/fields/#django.forms.FloatField\" title=\"django.forms.FloatField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">FloatField</span></code></a> convert\nvalues to a Python <code class=\"docutils literal notranslate\"><span class=\"pre\">int</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">float</span></code> respectively.</p>\n<p>Here’s how the form data could be processed in the view that handles this form:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">views.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</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 code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.mail</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">send_mail</span>\n\n<span class=\"k\">if</span> <span class=\"n\">form</span><span class=\"o\">.</span><span class=\"n\">is_valid</span><span class=\"p\">():</span>\n    <span class=\"n\">subject</span> <span class=\"o\">=</span> <span class=\"n\">form</span><span class=\"o\">.</span><span class=\"n\">cleaned_data</span><span class=\"p\">[</span><span class=\"s1\">&#39;subject&#39;</span><span class=\"p\">]</span>\n    <span class=\"n\">message</span> <span class=\"o\">=</span> <span class=\"n\">form</span><span class=\"o\">.</span><span class=\"n\">cleaned_data</span><span class=\"p\">[</span><span class=\"s1\">&#39;message&#39;</span><span class=\"p\">]</span>\n    <span class=\"n\">sender</span> <span class=\"o\">=</span> <span class=\"n\">form</span><span class=\"o\">.</span><span class=\"n\">cleaned_data</span><span class=\"p\">[</span><span class=\"s1\">&#39;sender&#39;</span><span class=\"p\">]</span>\n    <span class=\"n\">cc_myself</span> <span class=\"o\">=</span> <span class=\"n\">form</span><span class=\"o\">.</span><span class=\"n\">cleaned_data</span><span class=\"p\">[</span><span class=\"s1\">&#39;cc_myself&#39;</span><span class=\"p\">]</span>\n\n    <span class=\"n\">recipients</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s1\">&#39;info@example.com&#39;</span><span class=\"p\">]</span>\n    <span class=\"k\">if</span> <span class=\"n\">cc_myself</span><span class=\"p\">:</span>\n        <span class=\"n\">recipients</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"n\">sender</span><span class=\"p\">)</span>\n\n    <span class=\"n\">send_mail</span><span class=\"p\">(</span><span class=\"n\">subject</span><span class=\"p\">,</span> <span class=\"n\">message</span><span class=\"p\">,</span> <span class=\"n\">sender</span><span class=\"p\">,</span> <span class=\"n\">recipients</span><span class=\"p\">)</span>\n    <span class=\"k\">return</span> <span class=\"n\">HttpResponseRedirect</span><span class=\"p\">(</span><span class=\"s1\">&#39;/thanks/&#39;</span><span class=\"p\">)</span>\n</code></pre></figure>\n<aside class=\"admonition admonition-tip\">\n<p class=\"admonition-title\">Truco</p>\n<p>For more on sending email from Django, see <a class=\"reference internal\" href=\"/es/4.1/topics/email/\"><span class=\"doc\">Sending email</span></a>.</p>\n</aside>\n<p>Some field types need some extra handling. For example, files that are uploaded\nusing a form need to be handled differently (they can be retrieved from\n<code class=\"docutils literal notranslate\"><span class=\"pre\">request.FILES</span></code>, rather than <code class=\"docutils literal notranslate\"><span class=\"pre\">request.POST</span></code>). For details of how to handle\nfile uploads with your form, see <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#binding-uploaded-files\"><span class=\"std std-ref\">Binding uploaded files to a form</span></a>.</p>\n</section>\n</section>\n</section>\n<section id=\"working-with-form-templates\">\n<h2>Working with form templates<a class=\"heading-anchor\" href=\"#working-with-form-templates\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>All you need to do to get your form into a template is to place the form\ninstance into the template context. So if your form is called <code class=\"docutils literal notranslate\"><span class=\"pre\">form</span></code> in the\ncontext, <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">form</span> <span class=\"pre\">}}</span></code> will render its <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input&gt;</span></code> elements\nappropriately.</p>\n<aside class=\"admonition-additional-form-template-furniture admonition\">\n<p class=\"admonition-title\">Additional form template furniture</p>\n<p>Don’t forget that a form’s output does <em>not</em> include the surrounding\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;form&gt;</span></code> tags, or the form’s <code class=\"docutils literal notranslate\"><span class=\"pre\">submit</span></code> control. You will have to provide\nthese yourself.</p>\n</aside>\n<section id=\"reusable-form-templates\">\n<h3>Reusable form templates<a class=\"heading-anchor\" href=\"#reusable-form-templates\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The HTML output when rendering a form is itself generated via a template. You\ncan control this by creating an appropriate template file and setting a custom\n<a class=\"reference internal\" href=\"/es/4.1/ref/settings/#std-setting-FORM_RENDERER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">FORM_RENDERER</span></code></a> to use that\n<a class=\"reference internal\" href=\"/es/4.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> site-wide. You\ncan also customize per-form by overriding the form’s\n<a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#django.forms.Form.template_name\" title=\"django.forms.Form.template_name\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">template_name</span></code></a> attribute to render the form using the\ncustom template, or by passing the template name directly to\n<a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#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>.</p>\n<p>The example below will result in <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">form</span> <span class=\"pre\">}}</span></code> being rendered as the output of\nthe <code class=\"docutils literal notranslate\"><span class=\"pre\">form_snippet.html</span></code> template.</p>\n<p>In your templates:</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># In your template:\n<span class=\"cp\">{{</span> <span class=\"nv\">form</span> <span class=\"cp\">}}</span>\n\n# In form_snippet.html:\n<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=\"p\">&lt;</span><span class=\"nt\">div</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;fieldWrapper&quot;</span><span class=\"p\">&gt;</span>\n        <span class=\"cp\">{{</span> <span class=\"nv\">field.errors</span> <span class=\"cp\">}}</span>\n        <span class=\"cp\">{{</span> <span class=\"nv\">field.label_tag</span> <span class=\"cp\">}}</span> <span class=\"cp\">{{</span> <span class=\"nv\">field</span> <span class=\"cp\">}}</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>Then you can configure the <a class=\"reference internal\" href=\"/es/4.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<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">settings.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</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 code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.forms.renderers</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">TemplatesSetting</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">CustomFormRenderer</span><span class=\"p\">(</span><span class=\"n\">TemplatesSetting</span><span class=\"p\">):</span>\n    <span class=\"n\">form_template_name</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;form_snippet.html&quot;</span>\n\n<span class=\"n\">FORM_RENDERER</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;project.settings.CustomFormRenderer&quot;</span>\n</code></pre></figure>\n<p>… or for a single form:</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\">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\">template_name</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;form_snippet.html&quot;</span>\n    <span class=\"o\">...</span>\n</code></pre></div>\n<p>… or for a single render of a form instance, passing in the template name to\nthe <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#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>. Here’s an example of this being used in a view:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">index</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"n\">form</span> <span class=\"o\">=</span> <span class=\"n\">MyForm</span><span class=\"p\">()</span>\n    <span class=\"n\">rendered_form</span> <span class=\"o\">=</span> <span class=\"n\">form</span><span class=\"o\">.</span><span class=\"n\">render</span><span class=\"p\">(</span><span class=\"s2\">&quot;form_snippet.html&quot;</span><span class=\"p\">)</span>\n    <span class=\"n\">context</span> <span class=\"o\">=</span> <span class=\"p\">{</span><span class=\"s1\">&#39;form&#39;</span><span class=\"p\">:</span> <span class=\"n\">rendered_form</span><span class=\"p\">}</span>\n    <span class=\"k\">return</span> <span class=\"n\">render</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">,</span> <span class=\"s1\">&#39;index.html&#39;</span><span class=\"p\">,</span> <span class=\"n\">context</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>See <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#ref-forms-api-outputting-html\"><span class=\"std std-ref\">Outputting forms as HTML</span></a> for more details.</p>\n<aside class=\"version-note version-changed\" data-version=\"4.0\">\n<p class=\"version-note-title\">Changed in Django 4.0</p><p>Template rendering of forms was added.</p>\n</aside>\n<aside class=\"version-note version-changed\" data-version=\"4.1\">\n<p class=\"version-note-title\">Changed in Django 4.1</p><p>The ability to set the default <code class=\"docutils literal notranslate\"><span class=\"pre\">form_template_name</span></code> on the form renderer\nwas added.</p>\n</aside>\n</section>\n<section id=\"form-rendering-options\">\n<h3>Form rendering options<a class=\"heading-anchor\" href=\"#form-rendering-options\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>There are other output options though for the <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code>/<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input&gt;</span></code> pairs:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">form.as_div</span> <span class=\"pre\">}}</span></code> will render them wrapped in <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;div&gt;</span></code> tags.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">form.as_table</span> <span class=\"pre\">}}</span></code> will render them as table cells wrapped in <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;tr&gt;</span></code>\ntags.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">form.as_p</span> <span class=\"pre\">}}</span></code> will render them wrapped in <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;p&gt;</span></code> tags.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">form.as_ul</span> <span class=\"pre\">}}</span></code> will render them wrapped in <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;li&gt;</span></code> tags.</p></li>\n</ul>\n<p>Note that you’ll have to provide the surrounding <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;table&gt;</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;ul&gt;</span></code>\nelements yourself.</p>\n<p>Here’s the output of <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">form.as_p</span> <span class=\"pre\">}}</span></code> for our <code class=\"docutils literal notranslate\"><span class=\"pre\">ContactForm</span></code> instance:</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\">p</span><span class=\"p\">&gt;&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\">id</span><span class=\"o\">=</span><span class=\"s\">&quot;id_subject&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;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=\"p\">&gt;&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">p</span><span class=\"p\">&gt;&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\">textarea</span> <span class=\"na\">name</span><span class=\"o\">=</span><span class=\"s\">&quot;message&quot;</span> <span class=\"na\">id</span><span class=\"o\">=</span><span class=\"s\">&quot;id_message&quot;</span> <span class=\"na\">required</span><span class=\"p\">&gt;&lt;/</span><span class=\"nt\">textarea</span><span class=\"p\">&gt;&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">p</span><span class=\"p\">&gt;&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\">id</span><span class=\"o\">=</span><span class=\"s\">&quot;id_sender&quot;</span> <span class=\"na\">required</span><span class=\"p\">&gt;&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">p</span><span class=\"p\">&gt;&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;&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>Note that each form field has an ID attribute set to <code class=\"docutils literal notranslate\"><span class=\"pre\">id_&lt;field-name&gt;</span></code>, which\nis referenced by the accompanying label tag. This is important in ensuring that\nforms are accessible to assistive technology such as screen reader software.\nYou can also <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#ref-forms-api-configuring-label\"><span class=\"std std-ref\">customize the way in which labels and ids are generated</span></a>.</p>\n<p>See <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#ref-forms-api-outputting-html\"><span class=\"std std-ref\">Outputting forms as HTML</span></a> for more on this.</p>\n</section>\n<section id=\"rendering-fields-manually\">\n<h3>Rendering fields manually<a class=\"heading-anchor\" href=\"#rendering-fields-manually\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>We don’t have to let Django unpack the form’s fields; we can do it manually if\nwe like (allowing us to reorder the fields, for example). Each field is\navailable as an attribute of the form using <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">form.name_of_field</span> <span class=\"pre\">}}</span></code>, and\nin a Django template, will be rendered appropriately. 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=\"nv\">form.non_field_errors</span> <span class=\"cp\">}}</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">div</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;fieldWrapper&quot;</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">form.subject.errors</span> <span class=\"cp\">}}</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;</span><span class=\"cp\">{{</span> <span class=\"nv\">form.subject.id_for_label</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>Email subject:<span class=\"p\">&lt;/</span><span class=\"nt\">label</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">form.subject</span> <span class=\"cp\">}}</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=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;fieldWrapper&quot;</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">form.message.errors</span> <span class=\"cp\">}}</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;</span><span class=\"cp\">{{</span> <span class=\"nv\">form.message.id_for_label</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>Your message:<span class=\"p\">&lt;/</span><span class=\"nt\">label</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">form.message</span> <span class=\"cp\">}}</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=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;fieldWrapper&quot;</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">form.sender.errors</span> <span class=\"cp\">}}</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;</span><span class=\"cp\">{{</span> <span class=\"nv\">form.sender.id_for_label</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>Your email address:<span class=\"p\">&lt;/</span><span class=\"nt\">label</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">form.sender</span> <span class=\"cp\">}}</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=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;fieldWrapper&quot;</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">form.cc_myself.errors</span> <span class=\"cp\">}}</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;</span><span class=\"cp\">{{</span> <span class=\"nv\">form.cc_myself.id_for_label</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>CC yourself?<span class=\"p\">&lt;/</span><span class=\"nt\">label</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">form.cc_myself</span> <span class=\"cp\">}}</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>Complete <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code> elements can also be generated using the\n<a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#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>. 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=\"p\">&lt;</span><span class=\"nt\">div</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;fieldWrapper&quot;</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">form.subject.errors</span> <span class=\"cp\">}}</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">form.subject.label_tag</span> <span class=\"cp\">}}</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">form.subject</span> <span class=\"cp\">}}</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<section id=\"rendering-form-error-messages\">\n<h4>Rendering form error messages<a class=\"heading-anchor\" href=\"#rendering-form-error-messages\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>The price of this flexibility is a bit more work. Until now we haven’t had to\nworry about how to display form errors, because that’s taken care of for us. In\nthis example we have had to make sure we take care of any errors for each field\nand any errors for the form as a whole. Note <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">form.non_field_errors</span> <span class=\"pre\">}}</span></code> at\nthe top of the form and the template lookup for errors on each field.</p>\n<p>Using <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">form.name_of_field.errors</span> <span class=\"pre\">}}</span></code> displays a list of form errors,\nrendered as an unordered list. This might look like:</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\">ul</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;errorlist&quot;</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>Sender is required.<span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>The list has a CSS class of <code class=\"docutils literal notranslate\"><span class=\"pre\">errorlist</span></code> to allow you to style its appearance.\nIf you wish to further customize the display of errors you can do so by looping\nover them:</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.subject.errors</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">ol</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">error</span> <span class=\"k\">in</span> <span class=\"nv\">form.subject.errors</span> <span class=\"cp\">%}</span>\n        <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;&lt;</span><span class=\"nt\">strong</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">error</span><span class=\"o\">|</span><span class=\"nf\">escape</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">strong</span><span class=\"p\">&gt;&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">ol</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>Non-field errors (and/or hidden field errors that are rendered at the top of\nthe form when using helpers like <code class=\"docutils literal notranslate\"><span class=\"pre\">form.as_p()</span></code>) will be rendered with an\nadditional class of <code class=\"docutils literal notranslate\"><span class=\"pre\">nonfield</span></code> to help distinguish them from field-specific\nerrors. For example, <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">form.non_field_errors</span> <span class=\"pre\">}}</span></code> would look like:</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\">ul</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;errorlist nonfield&quot;</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>Generic validation error<span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>See <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/\"><span class=\"doc\">The Forms API</span></a> for more on errors, styling, and working with form\nattributes in templates.</p>\n</section>\n</section>\n<section id=\"looping-over-the-form-s-fields\">\n<h3>Looping over the form’s fields<a class=\"heading-anchor\" href=\"#looping-over-the-form-s-fields\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you’re using the same HTML for each of your form fields, you can reduce\nduplicate code by looping through each field in turn using a <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">for</span> <span class=\"pre\">%}</span></code>\nloop:</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=\"p\">&lt;</span><span class=\"nt\">div</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;fieldWrapper&quot;</span><span class=\"p\">&gt;</span>\n        <span class=\"cp\">{{</span> <span class=\"nv\">field.errors</span> <span class=\"cp\">}}</span>\n        <span class=\"cp\">{{</span> <span class=\"nv\">field.label_tag</span> <span class=\"cp\">}}</span> <span class=\"cp\">{{</span> <span class=\"nv\">field</span> <span class=\"cp\">}}</span>\n        <span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">field.help_text</span> <span class=\"cp\">%}</span>\n        <span class=\"p\">&lt;</span><span class=\"nt\">p</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;help&quot;</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">field.help_text</span><span class=\"o\">|</span><span class=\"nf\">safe</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n        <span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>Useful attributes on <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">field</span> <span class=\"pre\">}}</span></code> include:</p>\n<dl class=\"simple\">\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">field.errors</span> <span class=\"pre\">}}</span></code></dt><dd><p>Outputs a <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;ul</span> <span class=\"pre\">class=&quot;errorlist&quot;&gt;</span></code> containing any validation errors\ncorresponding to this field. You can customize the presentation of\nthe errors with a <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">for</span> <span class=\"pre\">error</span> <span class=\"pre\">in</span> <span class=\"pre\">field.errors</span> <span class=\"pre\">%}</span></code> loop. In this\ncase, each object in the loop is a string containing the error message.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">field.field</span> <span class=\"pre\">}}</span></code></dt><dd><p>The <a class=\"reference internal\" href=\"/es/4.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=\"/es/4.1/ref/forms/api/#django.forms.BoundField\" title=\"django.forms.BoundField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">BoundField</span></code></a> wraps. You can use it to access\n<a class=\"reference internal\" href=\"/es/4.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> attributes, e.g.\n<code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">char_field.field.max_length</span> <span class=\"pre\">}}</span></code>.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">field.help_text</span> <span class=\"pre\">}}</span></code></dt><dd><p>Any help text that has been associated with the field.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">field.html_name</span> <span class=\"pre\">}}</span></code></dt><dd><p>The name of the field that will be used in the input element’s name\nfield. This takes the form prefix into account, if it has been set.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">field.id_for_label</span> <span class=\"pre\">}}</span></code></dt><dd><p>The ID that will be used for this field (<code class=\"docutils literal notranslate\"><span class=\"pre\">id_email</span></code> in the example\nabove). If you are constructing the label manually, you may want to use\nthis in lieu of <code class=\"docutils literal notranslate\"><span class=\"pre\">label_tag</span></code>. It’s also useful, for example, if you have\nsome inline JavaScript and want to avoid hardcoding the field’s ID.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">field.is_hidden</span> <span class=\"pre\">}}</span></code></dt><dd><p>This attribute is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> if the form field is a hidden field and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code> otherwise. It’s not particularly useful as a template\nvariable, but could be useful in conditional tests such as:</p>\n</dd>\n</dl>\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\">field.is_hidden</span> <span class=\"cp\">%}</span>\n   <span class=\"c\">{# Do something special #}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<dl>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">field.label</span> <span class=\"pre\">}}</span></code></dt><dd><p>The label of the field, e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">Email</span> <span class=\"pre\">address</span></code>.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">field.label_tag</span> <span class=\"pre\">}}</span></code></dt><dd><p>The field’s label wrapped in the appropriate HTML <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code> tag. This\nincludes the form’s <a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#django.forms.Form.label_suffix\" title=\"django.forms.Form.label_suffix\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">label_suffix</span></code></a>. For example,\nthe default <code class=\"docutils literal notranslate\"><span class=\"pre\">label_suffix</span></code> is a colon:</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=\"o\">&lt;</span><span class=\"n\">label</span> <span class=\"k\">for</span><span class=\"o\">=</span><span class=\"s2\">&quot;id_email&quot;</span><span class=\"o\">&gt;</span><span class=\"n\">Email</span> <span class=\"n\">address</span><span class=\"p\">:</span><span class=\"o\">&lt;/</span><span class=\"n\">label</span><span class=\"o\">&gt;</span>\n</code></pre></div>\n</dd>\n</dl>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">field.legend_tag</span> <span class=\"pre\">}}</span></code></p>\n<blockquote>\n<div><aside class=\"version-note version-added\" data-version=\"4.1\">\n<p class=\"version-note-title\">New in Django 4.1</p></aside>\n<p>Similar to <code class=\"docutils literal notranslate\"><span class=\"pre\">field.label_tag</span></code> but uses a <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;legend&gt;</span></code> tag in place of\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;label&gt;</span></code>, for widgets with multiple inputs wrapped in a <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;fieldset&gt;</span></code>.</p>\n</div></blockquote>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">field.use_fieldset</span> <span class=\"pre\">}}</span></code></p>\n<blockquote>\n<div><aside class=\"version-note version-added\" data-version=\"4.1\">\n<p class=\"version-note-title\">New in Django 4.1</p></aside>\n<p>This attribute is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> if the form field’s widget contains multiple\ninputs that should be semantically grouped in a <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;fieldset&gt;</span></code> with a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;legend&gt;</span></code> to improve accessibility. An example use in a template:</p>\n</div></blockquote>\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\">field.use_fieldset</span> <span class=\"cp\">%}</span>\n  <span class=\"p\">&lt;</span><span class=\"nt\">fieldset</span><span class=\"p\">&gt;</span>\n  <span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">field.label</span> <span class=\"cp\">%}{{</span> <span class=\"nv\">field.legend_tag</span> <span class=\"cp\">}}{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">else</span> <span class=\"cp\">%}</span>\n  <span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">field.label</span> <span class=\"cp\">%}{{</span> <span class=\"nv\">field.label_tag</span> <span class=\"cp\">}}{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{{</span> <span class=\"nv\">field</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">field.use_fieldset</span> <span class=\"cp\">%}</span><span class=\"p\">&lt;/</span><span class=\"nt\">fieldset</span><span class=\"p\">&gt;</span><span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<dl class=\"simple\">\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">field.value</span> <span class=\"pre\">}}</span></code></dt><dd><p>The value of the field. e.g <code class=\"docutils literal notranslate\"><span class=\"pre\">someone&#64;example.com</span></code>.</p>\n</dd>\n</dl>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">Ver también</p>\n<p>For a complete list of attributes and methods, see\n<a class=\"reference internal\" href=\"/es/4.1/ref/forms/api/#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</aside>\n<section id=\"looping-over-hidden-and-visible-fields\">\n<h4>Looping over hidden and visible fields<a class=\"heading-anchor\" href=\"#looping-over-hidden-and-visible-fields\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>If you’re manually laying out a form in a template, as opposed to relying on\nDjango’s default form layout, you might want to treat <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input</span> <span class=\"pre\">type=&quot;hidden&quot;&gt;</span></code>\nfields differently from non-hidden fields. For example, because hidden fields\ndon’t display anything, putting error messages «next to» the field could cause\nconfusion for your users – so errors for those fields should be handled\ndifferently.</p>\n<p>Django provides two methods on a form that allow you to loop over the hidden\nand visible fields independently: <code class=\"docutils literal notranslate\"><span class=\"pre\">hidden_fields()</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">visible_fields()</span></code>. Here’s a modification of an earlier example that uses\nthese two methods:</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=\"c\">{# Include the hidden fields #}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">hidden</span> <span class=\"k\">in</span> <span class=\"nv\">form.hidden_fields</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{{</span> <span class=\"nv\">hidden</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n<span class=\"c\">{# Include the visible fields #}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">field</span> <span class=\"k\">in</span> <span class=\"nv\">form.visible_fields</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">div</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;fieldWrapper&quot;</span><span class=\"p\">&gt;</span>\n        <span class=\"cp\">{{</span> <span class=\"nv\">field.errors</span> <span class=\"cp\">}}</span>\n        <span class=\"cp\">{{</span> <span class=\"nv\">field.label_tag</span> <span class=\"cp\">}}</span> <span class=\"cp\">{{</span> <span class=\"nv\">field</span> <span class=\"cp\">}}</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>This example does not handle any errors in the hidden fields. Usually, an\nerror in a hidden field is a sign of form tampering, since normal form\ninteraction won’t alter them. However, you could easily insert some error\ndisplays for those form errors, as well.</p>\n</section>\n</section>\n</section>\n<section id=\"further-topics\">\n<h2>Further topics<a class=\"heading-anchor\" href=\"#further-topics\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>This covers the basics, but forms can do a whole lot more:</p>\n<div class=\"toctree-wrapper compound\">\n<ul>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/formsets/\">Formsets</a><ul>\n<li class=\"toctree-l2\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/formsets/#using-initial-data-with-a-formset\">Using initial data with a formset</a></li>\n<li class=\"toctree-l2\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/formsets/#limiting-the-maximum-number-of-forms\">Limiting the maximum number of forms</a></li>\n<li class=\"toctree-l2\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/formsets/#limiting-the-maximum-number-of-instantiated-forms\">Limiting the maximum number of instantiated forms</a></li>\n<li class=\"toctree-l2\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/formsets/#formset-validation\">Formset validation</a></li>\n<li class=\"toctree-l2\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/formsets/#validating-the-number-of-forms-in-a-formset\">Validating the number of forms in a formset</a></li>\n<li class=\"toctree-l2\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/formsets/#dealing-with-ordering-and-deletion-of-forms\">Dealing with ordering and deletion of forms</a></li>\n<li class=\"toctree-l2\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/formsets/#adding-additional-fields-to-a-formset\">Adding additional fields to a formset</a></li>\n<li class=\"toctree-l2\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/formsets/#passing-custom-parameters-to-formset-forms\">Passing custom parameters to formset forms</a></li>\n<li class=\"toctree-l2\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/formsets/#customizing-a-formset-s-prefix\">Customizing a formset’s prefix</a></li>\n<li class=\"toctree-l2\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/formsets/#using-a-formset-in-views-and-templates\">Using a formset in views and templates</a></li>\n</ul>\n</li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/modelforms/\">Creating forms from models</a><ul>\n<li class=\"toctree-l2\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/modelforms/#modelform\"><code class=\"docutils literal notranslate\"><span class=\"pre\">ModelForm</span></code></a></li>\n<li class=\"toctree-l2\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/modelforms/#model-formsets\">Model formsets</a></li>\n<li class=\"toctree-l2\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/modelforms/#inline-formsets\">Inline formsets</a></li>\n</ul>\n</li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/media/\">Form Assets (the <code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> class)</a><ul>\n<li class=\"toctree-l2\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/media/#assets-as-a-static-definition\">Assets as a static definition</a></li>\n<li class=\"toctree-l2\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/media/#media-as-a-dynamic-property\"><code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> as a dynamic property</a></li>\n<li class=\"toctree-l2\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/media/#paths-in-asset-definitions\">Paths in asset definitions</a></li>\n<li class=\"toctree-l2\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/media/#media-objects\"><code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> objects</a></li>\n<li class=\"toctree-l2\"><a class=\"reference internal\" href=\"/es/4.1/topics/forms/media/#media-on-forms\"><code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> on Forms</a></li>\n</ul>\n</li>\n</ul>\n</div>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">Ver también</p>\n<dl class=\"simple\">\n<dt><a class=\"reference internal\" href=\"/es/4.1/ref/forms/\"><span class=\"doc\">The Forms Reference</span></a></dt><dd><p>Covers the full API reference, including form fields, form widgets,\nand form and field validation.</p>\n</dd>\n</dl>\n</aside>\n</section>","rootId":"working-with-forms","toc":[{"title":"HTML forms","anchor":"html-forms","children":[{"title":"GET and POST","anchor":"get-and-post","children":[]}]},{"title":"Django’s role in forms","anchor":"django-s-role-in-forms","children":[]},{"title":"Forms in Django","anchor":"forms-in-django","children":[{"title":"The Django Form class","anchor":"the-django-form-class","children":[]},{"title":"Instantiating, processing, and rendering forms","anchor":"instantiating-processing-and-rendering-forms","children":[]}]},{"title":"Building a form","anchor":"building-a-form","children":[{"title":"The work that needs to be done","anchor":"the-work-that-needs-to-be-done","children":[]},{"title":"Building a form in Django","anchor":"building-a-form-in-django","children":[{"title":"The Form class","anchor":"the-form-class","children":[]},{"title":"The view","anchor":"the-view","children":[]},{"title":"The template","anchor":"the-template","children":[]}]}]},{"title":"More about Django Form classes","anchor":"more-about-django-form-classes","children":[{"title":"Bound and unbound form instances","anchor":"bound-and-unbound-form-instances","children":[]},{"title":"More on fields","anchor":"more-on-fields","children":[{"title":"Widgets","anchor":"widgets","children":[]},{"title":"Field data","anchor":"field-data","children":[]}]}]},{"title":"Working with form templates","anchor":"working-with-form-templates","children":[{"title":"Reusable form templates","anchor":"reusable-form-templates","children":[]},{"title":"Form rendering options","anchor":"form-rendering-options","children":[]},{"title":"Rendering fields manually","anchor":"rendering-fields-manually","children":[{"title":"Rendering form error messages","anchor":"rendering-form-error-messages","children":[]}]},{"title":"Looping over the form’s fields","anchor":"looping-over-the-form-s-fields","children":[{"title":"Looping over hidden and visible fields","anchor":"looping-over-hidden-and-visible-fields","children":[]}]}]},{"title":"Further topics","anchor":"further-topics","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"Using Django","url":"/es/4.1/topics/"}],"prev":{"docname":"topics/http/sessions","title":"How to use sessions","url":"/es/4.1/topics/http/sessions/"},"next":{"docname":"topics/forms/formsets","title":"Formsets","url":"/es/4.1/topics/forms/formsets/"},"formats":{"html":"/es/4.1/topics/forms/","markdown":"/es/4.1/topics/forms.md","json":"/es/4.1/topics/forms.json"},"source":"https://github.com/django/django/blob/stable/4.1.x/docs/topics/forms/index.txt","official":"https://docs.djangoproject.com/es/4.1/topics/forms/","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","1.9"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}