Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Further research turned up the fact that the RadioSelect <code>renderer</code> can be <em>replaced</em>, and passed in as a parameter to the initial widget creation function. So I created my own renderer, and attached a list of Booleans, indicating which Radio buttons should be disabled.</p> <p>The renderer adds a <em>disabled</em> attribute to each Radio button widget as needed, then the <em>force_unicode</em> turns the widget's attributes into HTML that include a <code>disabled="True"</code> value.</p> <pre><code>from django.utils.safestring import mark_safe from django.utils.encoding import force_unicode class RandomRenderer(forms.RadioSelect.renderer): """ Modifies some of the Radio buttons to be disabled in HTML, based on an externally-appended Actives list. """ def render(self): if not hasattr(self, "actives"): # oops, forgot to add an Actives list return self.original_render() return self.my_render() def original_render(self): return mark_safe(u'&lt;ul&gt;\n%s\n&lt;/ul&gt;' % u'\n'.join([u'&lt;li&gt;%s&lt;/li&gt;' % force_unicode(w) for w in self])) def my_render(self): midList = [] for x, wid in enumerate(self): if self.actives[x] == False: wid.attrs['disabled'] = True midList.append(u'&lt;li&gt;%s&lt;/li&gt;' % force_unicode(wid)) finalList = mark_safe(u'&lt;ul&gt;\n%s\n&lt;/ul&gt;' % u'\n'.join([u'&lt;li&gt;%s&lt;/li&gt;' % w for w in midList])) return finalList class OrderStatusForm(forms.Form): os = Order_Status.objects.values_list('id', 'status', 'reason') activeList = [True, False, True, False, True, False,] newStatus = forms.ChoiceField(widget=forms.RadioSelect( renderer=RandomRenderer), choices=os) newStatus.widget.renderer.actives = activeList </code></pre> <p>It's a little kludgy - I'm just sticking the <em>actives</em> list directly onto the renderer, which works (love Python's duck-typing), but would be cleaner if I passed the list in some constructors. Unfortunately I had problems with that, so took the easy way out. :)</p>
 

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