Don’t Die Trying With Django Forms

by Yuleidy Valladares

Forms are the best way to receive, process and save structured data from clients. Django offers tools to make this process easier, even though, some people die trying.

With HTML forms, you have to set each input with its respective attributes, check it, clean up the data, and save it. This process could be a complex task when you have many forms on your system. Django offers a tool to create a form like a class, so you can determine how it works, set fields, widgets and reuse them. This makes the homework simpler and automates part of the work. Awesome, doesn’t it?

A form is a subclass of Form or ModelForm, which are classes from Django. The class Form allows you to make flexible forms and determine how it works. Even associate it with several models or create your own validation rules. For example:

The class ModelForm creates your form from your model. This is a good point because most of the information you need is at the model (fields, labels, help text, max length). For example:

In your form class, you need to create the Meta class to define the model to use. Here, you can specify the fields to use

fields = ['name', 'is_shipping_required']

Fields to exclude

exclude = ['tax_rate']

If you want to use all the fields, you can set it like this

fields = '__all__'

Fields are an important part of a form because each one has its rule validations and check the input data. Every field has arguments to control its behavior like:

name = models.CharField(max_length=128, required=False)

If the required argument is not passed, the field assumes the value is required. Moreover, widget argument defines the widget to be rendered for the field, see more about widgets

status = form.ChoiceField(label='Status',widget=forms.RadioSelect())In the first example, there is a ChoiceField but its choices aren't defined. You can do this in a dynamic way, remember the form is a class and it has a constructor __init__

Depends on the previous status you can define the new choice for the field to be rendered in the form.

You can do this with queries from the database, for example:

A Django form flow is quite simple, a request is made from the client side, then, in the view, you have to check which type it is. If it's a GET request, it returns the default form (unbound or with initial values) to the template. The user fills the corresponding data and makes a POST request, in this case, in the same view, the data is validated. If they're correct, the remaining logic rules will be executed and it'll redirect to a success page. If it isn't, the form will be updated and rendered with the field errors.

Here is a graphic to understand better

You can create a clean method into your form class to define your own validation rules. For example, check the entered date is not before today.

This method does its default validations and then does your custom validations. It can be implemented by Form and ModelForm class.

The method save creates and saves an object from the database with the data filled in the form. The ModelForm has this method but you can rewrite it with your own rules, for example:

Django forms could be a little complex but with those basic steps, you can get started and do great things 💪

Saleor and AWS S3: Files with custom ACL and how to disable files overwriting
by Gregory Sánchez