當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。