Below is code subclassing the RadioSelect forms widget and associated
renderer, this allows radio button selection at runtime in a view.  I
intend to keep some user selections in session data, and this way the
users don't have to keep making the same radio button choices over and
over.  For example:

<pre>

# form class definition
class FeedbackForm(django.forms.Form):
    temp = django.forms.ChoiceField( widget=myWidgets.BR_RadioSelect
(), choices=[['S','Convert commas to spaces (1,2,0,3 yields 1 2 0
3)'], ['D','Use comma as decimal separator (1,203 yields 1.203)'],
['I','Remove commas from text (1,203 yields 1203)']] )


# use in a view, setting the desired button to be checked - in this
case the third radio button
    form = forms.FeedbackForm()
    form.fields['temp'].widget.renderer.indexOfItemToBeChecked = 2


#####################################
# code
#####################################

import django.forms.widgets
import django.utils.encoding
import django.utils.safestring

class BR_RadioFieldRenderer(django.forms.widgets.RadioFieldRenderer):
    indexOfItemToBeChecked = 0

    def render(self):
        itemIndex = 0
        itemString = ''
        for w in self:
            temp = u'%s<br>\n' % django.utils.encoding.force_unicode
(w)
            if itemIndex == self.indexOfItemToBeChecked:
                temp = temp.replace('/>', 'CHECKED />')
            itemString += temp
            itemIndex += 1
        return django.utils.safestring.mark_safe(itemString)


class BR_RadioSelect(django.forms.widgets.RadioSelect):
    renderer = BR_RadioFieldRenderer

</pre>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to