reading notes for code fellows
An HTML form is a group of fields used to collect information on a web page. Django forms take out a lot of the hassle by providing a framework for forms that will generate HTML and gather data according to your specifications. With Django forms, your view gets a request, performs any required actions, and then generates an HTML page that recieves the context containing the data that needs to be displayed. It will display the default form, receive data from a submit and bind it to the form, and clean and validate the data. Then, if any data is invalid, it will re-display the form with error messages or perform the correct actions for the data, such as save it, if everything is correct and redirect the user to another page. The most fundamental tool Django provides for this task is the Form class. This class specifies the fields in the form, the layout, initial values, etc. and provides the ability to render itself.
Declaring a Form is similar to declaring a Model. All you need is to import the forms library and have the class extend forms.Form. Then you will define your form fields, which can be things like DateField, ChoiceField, ImageField and so on. You can also have optional arguments on fields like required or initial.
Django also offers many ways to validate your date. If you use clean_field_name() it will clean the data of the specified field. You also can get your data through self.cleaned_data['name']. If a value falls outside the range of clean data, you can throw a ValidationError and specify the error text you want to display to the user.
Before you create a view for the form, you can go through URL configuration to redirest URLs with a specific format to a specified function. The view itself needs to render the default form and be able to rerender it and/or display error messages as necessary, as well as process the data and redirect the user. If you want to restrict access to a certain view, you can use @login_required and the @permission_required decorator to allow access.
If you only need the form to map fields for a single model, you can use the ModelForm helper class to define form information within the model itself.
I will be interested to see how easy it is to make forms in Django as opposed to HTML and React. I feel like each time we learn a new way to achieve something it gets easier and I hope that trend continues with this.