Setting help_text for each selection in RadioSelect

I can set the help_text attribute in any form field, but is it possible to set help_text for the selection used for RadioSelect ()?

I would be looking for a clean way to display some help information under each switch.

Below is the model and form code, I can display the name attribute in the template with a label, input element and help text. I would also like to provide a membership_type attribute with a label ("Type of membership"), radio buttons ("open membership" and "closed membership") and help text associated with each radio element ("anyone can join this group 'and' only 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 } 
+7
python django
source share
2 answers

@Rishab is correct , but I will clarify further, because, at first glance, this does not look like a solution, although it does; or, at least, it can be discarded in order to obtain a useful effect, without diving too deep into the forms of django.

The second element of the tuple is represented inside the "label" tag, so any "inline elements" are allowed; eg:

Desired Result

Or something like this

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

Simple example

The trick is that "mark_safe" contains the contents of the description, and then all you need is:

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

Comprehensive Example

So in this example we are:

  • put options into a list (any iterable structure will do)
  • pass structure to init form to create our radio parameters on the fly
  • use the concept list to create an extended description for each radio parameter

Data Structure: Tickets are my own classes, and they have attributes:

  • ticket.code - as in the ticket code
  • label - short description
  • help - a more detailed description

But more on that later. First, 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) 

This probably happened in your view code. Now, to find out 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() ) 
+6
source share

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

 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 is not a strict use of help_text , but in most cases it should be executed.

0
source share

All Articles