Django Form的基本实现机制和基本概念
本文只是理清Django Form系统中的关键概念和关键流程,没有关注其中的细节实现,
目的是对Djano的Form实现机制能有一个直观的了解,能够从从整体上把握它。
如果想深入了解代码实现及各种技术细节,可以阅读Django官方文档。
1 为什么要使用Django Form? 什么情况下要使用Django Form?
Unless you’re planning to build websites and applications that do nothing but publish content, and don’t accept input from your visitors, you’re going to need to understand and use forms.
2 基本概念:What is HTML Form?
HTML Form参考:w3school HTML Form
A form must specify two things:
- where: the URL to which the data corresponding to the user’s input should be returned
- how: the HTTP method the data should be returned by
3 HTTP GET/POST
都可以用来提交表单数据,
是否要在服务器中保存数据,是否要考虑安全因素。
4 Django 处理表单的基本流程:
Django handles three distinct parts of the work involved in forms:
- preparing and restructuring data to make it ready for rendering
- creating HTML forms for the data
- receiving and processing submitted forms and data from the client
5 Form Class
At the heart of this system of components is Django’s Form class
a Form class describes a form and determines how it works and appears.
Django中基于Form Class的表单处理流程:
6 What is ModelForm? Why use it?
If you’re building a database-driven app, chances are you’ll have forms that map closely to Django models. For instance, you might have a BlogComment model, and you want to create a form that lets people submit comments(your form is going to be used to directly add or edit a Django model, ). In this case, it would be redundant to define the field types in your form, because you’ve already defined the fields in your model.
For this reason, Django provides a helper class that lets you create a Form class from a Django model.
a ModelForm can save you a great deal of time, effort, and code, because it will build a form, along with the appropriate fields and their attributes, from a Model class.
7 What is form fields?
A form class’s fields map to HTML form <input> elements.
- A form’s fields are themselves classes; they manage form data and perform validation when a form is submitted.
- A DateField and a FileField handle very different kinds of data and have to do different things with it.
8 What is Widgets?
A widget is Django’s representation of an HTML input element.
The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget.
Each form field has a corresponding Widget class, which in turn corresponds to an HTML form widget such as <input type="text">.
In most cases, the field will have a sensible default widget. For example, by default, a CharField will have a TextInput widget, that produces an <input type="text"> in the HTML. If you needed <textarea> instead, you’d specify the appropriate widget when defining your form field, as we have done for the message field.
9 Form Fields and Widgets:
Form fields ------------deal with the logic of input validation and are used directly in templates.
Widgets---------------- deal with rendering of HTML form input elements on the web page and extraction of raw submitted data.
However, widgets do need to be assigned to form fields.