当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Django Field.help_text用法及代码示例


本文介绍 django.forms.Field.help_text 的用法。

声明

Field.help_text

help_text 参数允许您为此 Field 指定说明性文本。如果您提供 help_text ,当 Field 由一种方便的 Form 方法(例如 as_ul() )呈现时,它将显示在 Field 旁边。

与模型字段的 help_text 一样,此值不是 automatically-generated 表单中的 HTML-escaped。

这是一个完整的示例Form,它为它的两个字段实现了help_text。我们指定了auto_id=False 来简化输出:

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

相关用法


注:本文由纯净天空筛选整理自djangoproject.com大神的英文原创作品 django.forms.Field.help_text。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。