Understanding Django Forms
Overview
Django provides several classes to facilitate the work of creating forms. This article’s goal is to ease the understanding of how Django forms work and show an example of its basic usage, prior to reading the official documentation.
Introduction
An HTML form is defined by <form>...</form>
tags. The elements between
them allow visitors to perform several tasks like enter text, select
options and manipulate objects, and finally then send that information
back to the server.
In Django, form interface elements can be plain HTML basic elements like checkboxes or text input, or complex elements like pop ups, date pickers, etc.
Django forms helps with three main task:
- Preparing and restructuring data to make it ready for rendering
- data of several different types may need to be prepared for display in a form
- Creating the actual HTML forms for the data
- Rendering data as HTML elements
- Creating interfaces to edit data
- Receiving and processing submitted forms and data from the client
- Return data to the server
- Validate, clean and save data
- Pass it for further processing
So a form in this context could refer to:
- HTML
<form>
: https://www.w3.org/TR/html51/sec-forms.html#sec-forms - A Django form, a declaration that extends from
django.forms.Form
class that produces the HTML form: https://docs.djangoproject.com/en/2.0/ref/forms/api/#django.forms.Form- it is used to describe a form
- specify how it works
- specify how it appears
One of the most used features of Django forms, is the ability to
generate an HTML form from Django Models, it process model fields, and
generate an HTML <input>
element for each field in a Form.
Each form field is generated in the browser by an user interface element called widget,
Methods
GET and POST are the only HTTP methods to use when dealing with forms.
POST method
This is what happens in form using a POST method:
While in GET method information is transmitted without encoding it:
Building a form
To use Form instances, we should use the web form, described by a
Django Form, processed by a view, and rendered as an HTML .:
validate data: if
form.is_valid()
then form’s data will populate thecleaned_data
attribute.process the view: it can be the same view which published the form or another one. For example in a view that generates and process a form the process would be:
The form could have data or not, depending if it has been prepopulated or if there was any error when processing it. Associating data to form fields is called binding data to the form.
To check if a form has data bound to it there is the method django.forms.Form.is_bound().
References
- Adding a Cancel button in Django class-based views, editing views and formsJuly 15, 2019
- Using Django Model Primary Key in Custom Forms THE RIGHT WAYJuly 13, 2019
- Django formset handling with class based views, custom errors and validationJuly 4, 2019
- How To Use Bootstrap 4 In Django FormsMay 25, 2018
- Understanding Django Forms
- How To Create A Form In DjangoJuly 29, 2016
Articles
Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.
Powered by SimpleIT Hugo Theme
·