Django表单的数据放到它的属性 cleaned_data

root
abc abc

class UserForm(forms.Form):
... username = forms.CharField(
... max_length=255,
... help_text="e.g., user@example.com",
... widget=forms.TextInput(
... attrs={"aria-describedby": "custom-description id_username_helptext"},
... ),
... )

from django import forms
class HelpTextContactForm(forms.Form):
... subject = forms.CharField(max_length=100, help_text="100 characters max.")
... message = forms.CharField()
... sender = forms.EmailField(help_text="A valid email address, please.")
... cc_myself = forms.BooleanField(required=False)
...
f = HelpTextContactForm(auto_id=False)
print(f)

<div>Subject:<div class="helptext">100 characters max.</div><input type="text" name="subject" maxlength="100" required></div> <div>Message:<input type="text" name="message" required></div> <div>Sender:<div class="helptext">A valid email address, please.</div><input type="email" name="sender" required></div> <div>Cc myself:<input type="checkbox" name="cc_myself"></div>

import datetime
class DateForm(forms.Form):
... day = forms.DateField(initial=datetime.date.today)
...
print(DateForm())

<div><label for="id_day">Day:</label><input type="text" name="day" value="2023-02-11" required id="id_day"></div>

以下为示例表单:

from django import formsclass ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField(widget=forms.Textarea)
sender = forms.EmailField()
cc_myself = forms.BooleanField(required=False)

A Form instance has an is_valid() method, which runs validation routines for all its fields. When this method is called, if all fields contain valid data, it will:

返回 True

将表单的数据放到它的属性 cleaned_data 中。

表单的 is_bound 属性将告诉您一张表单是否具有绑定的数据。