[翻译]  setting help_text for each choice in a RadioSelect

[CHINESE]  为RadioSelect中的每个选项设置help_text


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()?

我可以在任何表单字段上设置help_text属性,但是可以在用于RadioSelect()的选项上设置help_text吗?

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').

下面是模型和表单的代码,我可以使用标签,输入元素和帮助文本在模板中呈现name属性。我还希望能够使用标签(“会员类型”),单选按钮(“开放会员”和“封闭会员”)以及与每个无线电元素相关联的帮助文本呈现membership_type属性('任何人都可以加入此组'和'只有选择成员可以加入此组')。

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 }

2 个解决方案

#1


6  

@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.

@Rishabh是正确的,但我会进一步详细说明,乍一看,它似乎不是解决方案,尽管它是;或者,至少,它可以被克服以获得有用的效果,而不必深入django形式。

The second element of the tuple is presented inside the "label" tag - so any 'inline elements' are permissible; for example:

元组的第二个元素显示在“label”标签内 - 因此任何“内联元素”都是允许的;例如:

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:

诀窍是“mark_safe”描述的内容然后填写你需要的东西:

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:

所以在这个例子中我们:

  1. assemble the choices into a list (any iterable structure will do)
  2. 将选项组合成一个列表(任何可迭代的结构都可以)
  3. pass the structure to the form's init to create our radio options on the fly
  4. 将结构传递给表单的init以动态创建我们的无线电选项
  5. use a comprehension list to create an extended description for each radio option
  6. 使用理解列表为每个无线电选项创建扩展描述

The data structure: Tickets are my own classes and they have attributes:

数据结构:票证是我自己的类,它们具有以下属性:

  • tickets.code - as in a ticket code
  • tickets.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()
        )

#2


0  

Assuming you're using RadioSelect as a widget for forms.ChoiceField, you can do something like:

假设您使用RadioSelect作为forms.ChoiceField的小部件,您可以执行以下操作:

choices = (('1', 'First help_text here'),
           ('2', 'Second help_text here'),
           ('3', 'Third help_text here'),
          )

class MyForm(forms.Form):
    ...
    choice = forms.ChoiceField(widget = RadioSelect, choices = choices)

This isn't a strict use of help_text but it should get the job done in most cases.

这不是对help_text的严格使用,但它应该在大多数情况下完成工作。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号