Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I did a little digging, and found a lead in the answer to this <a href="https://stackoverflow.com/questions/1669845/customizing-radioselect" title="customizing-radioselect">similar post</a> that points to a somewhat outdated <a href="http://groups.google.com/group/django-users/msg/b60410692c7c60e2" rel="nofollow noreferrer" title="post in django-users">post in django-users</a>.</p> <p>Borrowing some code from the as_widget() method in <a href="https://code.djangoproject.com/browser/django/trunk/django/forms/forms.py" rel="nofollow noreferrer" title="django trunk forms.py">forms.py</a> I fashioned a method that I could add to my form in order to retrieve the RadioSelect widget's choices rendered as HTML.</p> <pre><code>class MyForm(forms.Form): MY_CHOICES = ( ('opt0', 'Option zero'), ('opt1', 'Option one'), ) myfield = forms.ChoiceField(widget=forms.RadioSelect, choices=MY_CHOICES) def myfield_choices(self): """ Returns myfield's widget's default renderer, which can be used to render the choices of a RadioSelect widget. """ field = self['myfield'] widget = field.field.widget attrs = {} auto_id = field.auto_id if auto_id and 'id' not in widget.attrs: attrs['id'] = auto_id name = field.html_name return widget.get_renderer(name, field.value(), attrs=attrs) </code></pre> <p>Then in the template you can access individual radio button choices like so:</p> <pre><code>&lt;ul&gt; &lt;li&gt; {{ myform.myfield_choices.0 }} My custom HTML that goes with choice 0 &lt;/li&gt; &lt;li&gt; {{ myform.myfield_choices.1 }} Different HTML that goes with choice 1 &lt;/li&gt; &lt;/ul&gt; </code></pre> <p>or </p> <pre><code>{% for choice in myform.myfield_choices %} &lt;div&gt; {{ choice }} &lt;/div&gt; {% endfor %} </code></pre> <p>I'm pretty sure this is a bad idea. It will likely stop working at some point as django evolves. It violates DRY by copying code from as_widget(). (TBH, I didn't take the time to fully understand the code from as_widget) I'm using it as a temporary hack only. Perhaps there is a better way that involves custom template tags. If so, please let me know. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload