Django表单字段汇总

Field.clean(value)[source]

虽然表单字段的Field类主要使用在Form类中,但也可以直接实例化它们来使用,以便更好地了解它们是如何工作的。每个Field的实例都有一个clean()方法,它接受一个参数,然后返回“清洁的”数据或者抛出一个django.forms.ValidationError异常:

>>> from django import forms
>>> f = forms.EmailField() >>> f.clean('[email protected]') '[email protected]' >>> f.clean('invalid email address') Traceback (most recent call last): ... ValidationError: ['Enter a valid email address.'] 

这个clean方法经常被我们用来在开发或测试过程中对数据进行验证和测试。

一、核心字段参数

以下的参数是每个Field类都可以使用的。

1. required

给字段添加必填属性,不能空着。

>>> from django import forms
>>> f = forms.CharField() >>> f.clean('foo') 'foo' >>> f.clean('') Traceback (most recent call last): ... ValidationError: ['This field is required.'] >>> f.clean(None) Traceback (most recent call last): ... ValidationError: ['This field is required.'] >>> f.clean(' ') ' ' >>> f.clean(0) '0' >>> f.clean(True) 'True' >>> f.clean(False) 'False' 

若要表示一个字段不是必需的,设置required=False:

>>> f = forms.CharField(required=False)
>>> f.clean('foo')
'foo'
>>> f.clean('')
''
>>> f.clean(None)
''
>>> f.clean(0)
'0'
>>> f.clean(True)
'True'
>>> f.clean(False)
'False'

2. label

label参数用来给字段添加‘人类友好’的提示信息。如果没有设置这个参数,那么就用字段的首字母大写名字。比如:

下面的例子,前两个字段有,最后的comment没有label参数:

>>> from django import forms
>>> class CommentForm(forms.Form): ... name = forms.CharField(label='Your name') ... url = forms.URLField(label='Your website', required=False) ... comment = forms.CharField() >>> f = CommentForm(auto_id=False) >>> print(f) <tr><th>Your name:th><td><input type="text" name="name" required />td>tr> <tr><th>Your website:th><td><input type="url" name="url" />td>tr> <tr><th>Comment:th><td><input type="text" name="comment" required />td>tr> 

3. label_suffix

Django默认为上面的label参数后面加个冒号后缀,如果想自定义,可以使用label_suffix参数。比如下面的例子用“?”代替了冒号:

>>> class ContactForm(forms.Form):
...     age = forms.IntegerField()
...     nationality = forms.CharField()
...     captcha_answer = forms.IntegerField(label='2 + 2', label_suffix=' =')
>>> f = ContactForm(label_suffix='?')
>>> print(f.as_p())

id="id_age" name="age" type="number" required />

id="id_nationality" name="nationality" type="text" required />

id="id_captcha_answer" name="captcha_answer" type="number" required />

4. initial

为HTML页面中表单元素定义初始值。也就是input元素的value参数的值,如下所示:

>>> from django import forms
>>> class CommentForm(forms.Form): ... name = forms.CharField(initial='Your name') ... url = forms.URLField(initial='http://') ... comment = forms.CharField() >>> f = CommentForm(auto_id=False) >>> print(f) <tr><th>Name:th><td><input type="text" name="name" value="Your name" required />td>tr> <tr><th>Url:th><td><input type="url" name="url" value="http://" required />td>tr> <tr><th>Comment:th><td><input type="text" name="comment" required />td>tr> 

你可能会问为什么不在渲染表单的时候传递一个包含初始化值的字典给它,不是更方便?因为如果这么做,你将触发表单的验证过程,此时输出的HTML页面将包含验证中产生的错误,如下所示:

>>> class CommentForm(forms.Form):
...     name = forms.CharField()
...     url = forms.URLField()
...     comment = forms.CharField()
>>> default_data = {'name': 'Your name', 'url': 'http://'}
>>> f = CommentForm(default_data, auto_id=False)
>>> print(f)
Name:type="text" name="name" value="Your name" required /> Url:
    class="errorlist">
  • Enter a valid URL.
type="url" name="url" value="http://" required /> Comment:
    class="errorlist">
  • This field is required.
type="text" name="comment" required />

这就是为什么initial参数只用在未绑定的表单上。

还要注意,如果提交表单时某个字段的值没有填写,initial的值不会作为“默认”的数据。initial值只用于原始表单的显示:

>>> class CommentForm(forms.Form):
...     name = forms.CharField(initial='Your name')
...     url = forms.URLField(initial='http://')
...     comment = forms.CharField()
>>> data = {'name': '', 'url': '', 'comment': 'Foo'}
>>> f = CommentForm(data)
>>> f.is_valid()
False
# The form does *not* fall back to using the initial values.
>>> f.errors
{'url': ['This field is required.'], 'name': ['This field is required.']}

除了常量之外,你还可以传递一个可调用的对象:

>>> import datetime
>>> class DateForm(forms.Form): ... day = forms.DateField(initial=datetime.date.today) >>> print(DateForm()) <tr><th>Day:th><td><input type="text" name="day" value="12/23/2008" required /><td>tr> 

5. widget

最重要的参数之一,指定渲染Widget时使用的widget类,也就是这个form字段在HTML页面中是显示为文本输入框?密码输入框?单选按钮?多选框?还是别的....

6. help_text

该参数用于设置字段的辅助描述文本。

>>> 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.as_table()) <tr><th>Subject:th><td><input type="text" name="subject" maxlength="100" required /><br /><span class="helptext">100 characters max.span>td>tr> <tr><th>Message:th><td><input type="text" name="message" required />td>tr> <tr><th>Sender:th><td><input type="email" name="sender" required /><br />A valid email address, please.td>tr> <tr><th>Cc myself:th><td><input type="checkbox" name="cc_myself" />td>tr> >>> print(f.as_ul())) <li>Subject: <input type="text" name="subject" maxlength="100" required /> <span class="helptext">100 characters max.span>li> <li>Message: <input 

转载于:https://www.cnblogs.com/navysummer/p/10200236.html

你可能感兴趣的:(Django表单字段汇总)