为RadioSelect中的每个选项设置help_text [英] setting help_text for each choice in a RadioSelect
问题描述
我可以在任何表单字段上设置help_text属性,但是可以在RadioSelect()的选项上设置help_text吗?
我会看一个干净的方法来显示每个单选按钮下的一些帮助信息。
以下是模型和表单的代码,我可以在模板中呈现名称属性标签,输入元素和帮助文本。我也希望能够使用标签('Membership Type'),单选按钮('open membership'和'closed membership')来呈现membership_type属性,并帮助与每个radio元素关联的文本(任何人都可以加入
类组(models.Model):
MEMBERSHIP_CHOICES =(
('O','Open membership'),
('C','Closed membership'),
)
name = models。 CharField(max_length = 255)
membership_type = models.CharField(max_length = 1,choices = MEMBERSHIP_CHOICES,default =O)
class GroupForm(forms.ModelForm):
name = forms.CharField(label =Group name,help_text =输入新组的名称)
class Meta:
model = Group
widgets = { membership_type:forms.RadioSelect}
@Rishabh是正确的,但我会详细说明呃呃看起来似乎不是解决方案,尽管如此;或者,至少可以被排除以获得有用的效果,而不必深入django形式。
元组的第二个元素显示在标签标签 - 因此,任何内联元素都是允许的;例如:
所需的结果
或类似的东西
< ul>
< li>< label for =id_ticket_0>
< input type =radioid =id_ticket_0value =PARTTIMEname =ticket>
< em>部分时间< / em>只在星期五晚上和星期六有效
< / label>< / li>
< li>< label for =id_ticket_1>
< input type =radioid =id_ticket_1value =DAYTIMEname =ticket>
< em> Casual< / em>仅限星期六有效
< / label>< / li>
< li>< label for =id_ticket_2>
< input type =radioid =id_ticket_2value =EARLYBIRDname =ticket>
< em> Early Bird< / em>周五,周六和周日有效。 2011年1月3日上午1点前预订$ 15折优惠
< / label>< / li>
< / ul>
简单的例子
到mark_safe的描述内容,然后填写你需要的东西:
从django.utils.safestring import mark_safe
choice =(
('1',mark_safe(u'< em> One< / em> |这是第一个选项,它是awesome')),
('2' mark_safe(u'< em> Two< / em> |这是第二个选项,好的)')
)
复杂的例子
所以在这个例子中我们:
数据结构:
门票是我自己的类,他们有属性: p>
- ticket.code - 如在票代码
- 标签 - 一个简洁的简短描述
- 帮助 - 更长的描述
首先,我可以创建一些实例:
从我的模块导入票
#所以让我们创建一个
fulltime =票('FULLTIME',160,'全职',
周一至周五有效)
parttime =票('PARTTIME',110,'全职',
有效)
daytime = ticket('DAYTIME',70,'Day Time',
仅在周末和公众假期有效)
#和把它们放在一个列表中,你喜欢
available_tickets = [fulltime,parttime,daytime]
#现在创建窗体
OrderForm(ticket = available_tickets)
这可能发生在您的视图代码中。现在看看在表单中发生什么
类OrderForm(ModelForm):
def __init __(self,* args,** kwargs):
self.tickets = kwargs.pop('tickets')
super(OrderForm,self).__ init __(* args,** kwargs)
choices = [(t.code,mark_safe(u'< em>%s< / em>%s'%(t.label,t.help)))
for t in self.tickets]
self.fields ['ticket'] = forms.ChoiceField(
choices = choices,
widget = forms.RadioSelect()
)
I can set the help_text attribute on any form field, but is it possible to set help_text on the choices used for a RadioSelect()?
I'd looking for a clean way to show some help information under each radio button.
Below is the code for the model and the form, I can render the name attribute in a template with the label, input element and help text. I'd also like to be able to render membership_type attribute with a label ('Membership Type'), radio buttons ('open membership' and 'closed membership'), and help text associated to each radio element ('anyone can join this group' and 'only select members can join this group').
class Group(models.Model):
MEMBERSHIP_CHOICES = (
('O', 'Open membership'),
('C', 'Closed membership'),
)
name = models.CharField(max_length=255)
membership_type = models.CharField(max_length=1, choices=MEMBERSHIP_CHOICES, default="O")
class GroupForm(forms.ModelForm):
name = forms.CharField(label="Group name", help_text="Enter a name for your new group")
class Meta:
model = Group
widgets = { "membership_type": forms.RadioSelect }
@Rishabh is correct but I'll elaborate further as, at first glance, it doesn't appear to be the solution, although it is; or, at least, it can be kludged to get a useful effect without having to dive too deep into django forms.
The second element of the tuple is presented inside the "label" tag - so any 'inline elements' are permissible; for example:
The desired result
Or something like it
<ul>
<li><label for="id_ticket_0">
<input type="radio" id="id_ticket_0" value="PARTTIME" name="ticket">
<em>Part Time</em> Valid on Friday Night and Saturday Only
</label></li>
<li><label for="id_ticket_1">
<input type="radio" id="id_ticket_1" value="DAYTIME" name="ticket">
<em>Casual</em> Valid on Saturday Only
</label></li>
<li><label for="id_ticket_2">
<input type="radio" id="id_ticket_2" value="EARLYBIRD" name="ticket">
<em>Early Bird</em> Valid on Friday, Saturday, and Sunday. $15 discount for booking before 1am January 3rd, 2011
</label></li>
</ul>
The simple example
The trick is to "mark_safe" the content of the description then stuff whatever you need into:
from django.utils.safestring import mark_safe
choices = (
('1', mark_safe(u'<em>One</em> | This is the first option. It is awesome')),
('2', mark_safe(u'<em>Two</em> | This is the second option. Good too.'))
)
The complex example
So in this example we:
- assemble the choices into a list (any iterable structure will do)
- pass the structure to the form's init to create our radio options on the fly
- use a comprehension list to create an extended description for each radio option
The data structure: Tickets are my own classes and they have attributes:
- tickets.code - as in a ticket code
- label - a pithy short description
- help - a longer description
But more about that later. First lets create some instances:
from mymodule import ticket
# so lets create a few
fulltime = ticket('FULLTIME',160,'Full Time',
"Valid Monday to Friday inclusive")
parttime = ticket('PARTTIME',110,'Full Time',
"Valid outside of business hours only")
daytime = ticket('DAYTIME',70,'Day Time',
"Valid only on weekends and public holidays")
# and put them together in a list any way you like
available_tickets = [fulltime, parttime, daytime]
# now create the form
OrderForm(tickets=available_tickets)
That probably happened in your view code. Now to see what happens in the form
class OrderForm(ModelForm):
def __init__(self, *args, **kwargs):
self.tickets = kwargs.pop('tickets')
super(OrderForm, self).__init__(*args, **kwargs)
choices = [(t.code, mark_safe(u'<em>%s</em> %s' % (t.label, t.help)))
for t in self.tickets]
self.fields['ticket'] = forms.ChoiceField(
choices = choices,
widget = forms.RadioSelect()
)
这篇关于为RadioSelect中的每个选项设置help_text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!