Notes on Django forms. A quick overview to show Django forms basic concepts.
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.
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:
- A Django form, a declaration that extends from
django.forms.Formclass 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,
GET and POST are the only HTTP methods to use when dealing with forms.
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 <form>.:
validate data: if
form.is_valid()then form’s data will populate the
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().