定义 forms.Form
声明字段及其控件
定义 forms.Form 是 CoddyKit 上的免费 Django Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Django Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Django Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Forms Exist
A Django form turns messy user input into clean Python data, handling rendering and validation in one tidy place. 📝
Subclass forms.Form
You declare a form by subclassing forms.Form. Each class attribute you add becomes a field on that form.
from django import forms
class ContactForm(forms.Form):
name = forms.CharField()Fields Are Class Attributes
Every field is a class attribute set to a field instance, like CharField or EmailField. The attribute name becomes the input name.
name = forms.CharField()
email = forms.EmailField()CharField for Text
Use CharField for short text. Set max_length to cap the size and render a sensible HTML input automatically.
subject = forms.CharField(max_length=120)EmailField Validates Format
An EmailField checks that the value looks like a real email address, so you do not write that validation yourself.
email = forms.EmailField()Required by Default
Every field is required unless you say otherwise. Pass required=False to make a field optional for the user.
phone = forms.CharField(required=False)label and help_text
Add a friendly label and help_text so users understand each field. Django shows them next to the input.
name = forms.CharField(label="Your name",
help_text="First and last")Choosing a Widget
A widget controls the HTML rendered for a field. Swap the default to get a textarea, password box, or select.
message = forms.CharField(
widget=forms.Textarea)More Field Types
Django ships many fields: IntegerField, BooleanField, DateField, and ChoiceField each map to the right input and parse the value for you.
age = forms.IntegerField()
agree = forms.BooleanField()ChoiceField and Options
A ChoiceField renders a dropdown from a list of value and label pairs you provide in choices.
plan = forms.ChoiceField(choices=[
("free", "Free"), ("pro", "Pro")])Instantiate the Form
Create an unbound form with no data to display a blank form, or pass data to bind it for validation.
form = ContactForm() # blank
form = ContactForm(request.POST)Quick Check
Let us see how forms are defined.
Recap
You subclass forms.Form and add fields as class attributes. Pick the right field type and widget, and Django handles the rest. ✅
常见问题解答
「定义 forms.Form」课时是免费的吗?
是的 — 「定义 forms.Form」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Django Academy 课程的其余内容,请升级到 CoddyKit PRO。 Django Academy 课程共包含 4 节课。
「定义 forms.Form」这节课中我会学到什么?
声明字段及其控件 你通过在浏览器中直接运行的动手代码来练习 Django Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Django Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Django Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「定义 forms.Form」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Django Academy 课中编写并运行代码吗?
能。每节 Django Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 定义 forms.Form
- is_valid 与 cleaned_data
- 在模板中渲染表单
- CSRF 防护与 POST 流程