Hey Retro,

My boss managed to get this working so I can't take the credit but
this works and it's pretty useful. Basically, he's overriding the
widget renderer and making each "choice" a separate list item, giving
each "choice" it's own ID, making it much easier to use CSS to format
the choices individually (which is what I needed this for).

In the relevant forms.py:
class CustomRadioRenderer(forms.RadioSelect.renderer):
    def __init__(self, *args, **kwargs):
        super(CustomRadioRenderer, self).__init__(*args, **kwargs)

    def render(self):
        """Outputs a <ul> for this set of radio fields."""
        return mark_safe(u'<ul>\n%s\n</ul>' % u'\n'.join([u'<li id="li_
%s_%s">%s</li>'
                % (w.attrs.get('id','radio'),w.index,force_unicode(w))
for w in self]))

In the form code that is returned to the view:
class myform(forms.ModelForm):
    myfield = forms.IntegerField(widget = forms.RadioSelect(renderer =
CustomRadioRenderer, choices=Profile.MYFIELD_CHOICES))

Hope this helps you.

Cheers, R


On May 26, 8:09 am, Retro486 <russell.bernha...@gmail.com> wrote:
> Ok, well I took at look at the two links and they were pretty much the
> same thing; just access to the choice list as text, not the checkbox
> generator.
>
> I was able to generate the checkboxes manually, but they break the
> form system silently and just don't work (don't map to the usertype
> object for some reason). The field id's are _exactly_ the same, so I
> have no clue how Django is fetching their values from the POST. /shrug
>
> This is as far as I got if anyone wants to expand (usertype is my
> checkboxselectmultiple field):
>
> {% for choice_id, choice_label in form.usertype.field.choices %}
>     <tr>
>         <td>
>             <label for="id_usertype_{{ forloop.counter0 }}"><input
> type="checkbox" name="usertype"
>                 value="{{ choice_id }}" id="id_usertype_
> {{ forloop.counter0 }}" />
>             {{ choice_label }}</label>
>         </td>
>     </tr>
> {% endfor %}
>
> It's very close in that it does extract the labels, but I really need
> to be able to generate the individual _trackable_ checkbox fields
> too... :/
>
> I suppose the normal fix is to just build three checkboxes into my
> form and modify my clean method to validate them. Easy enough, but I
> really wanted to make Django do that for me so I could keep the line-
> by-line error messages to make it clear to users where they goofed
> (and without using JS magic 'cause that's another way to do it if you
> go the "normal fix" route but I hate relying on things that can be
> voluntarily disabled).
>
> Thanks again,
>
> -R
>
> On May 25, 5:39 pm, Retro486 <russell.bernha...@gmail.com> wrote:
>
> > Thanks, Richard. I'll take a look at the links you sent and see if I
> > can't work something out. In the meantime I've dropped back to a
> > MultipleChoiceField. :(
>
> > I'll post my findings.
>
> > -R
>
> > On May 25, 9:03 am, Richard <screame...@gmail.com> wrote:
>
> > > Hey Retro, I'm struggling with the same thing. I've found two
> > > interesting links which take two different approaches although I can't
> > > get either to quite work (I think the template is just not seeing the
> > > actual control but it's not complaining loudly). But it's quite
> > > possible that you'll be able to get it 
> > > working:http://stackoverflow.com/questions/733880/iterate-over-choices-in-che......
>
> > > Please post if you find a solution and I will if I get it first  :-)
>
> > > Cheers.
>
> > > On May 23, 9:48 pm, Retro486 <russell.bernha...@gmail.com> wrote:
>
> > > > I'm using the CheckboxSelectMultiple widget in a MultipleChoiceField
> > > > and I need more control over where the field labels go and the field
> > > > themselves. So far this is the only field type I've had a problem
> > > > with. I've spent at least a few hours trying to find info on the web
> > > > to no avail.
>
> > > > I can't figure out how to iterate over the list of choices in my
> > > > template. Here's the snippet from my form object:
>
> > > > USERTYPES = (
> > > >         ('f', 'Fan'),
> > > >         ('b', 'Band'),
> > > >         ('v', 'Venue'),
> > > >     )
> > > >     usertype = forms.MultipleChoiceField(choices=USERTYPES,
> > > >         widget=forms.CheckboxSelectMultiple())
>
> > > > And here's the snippet from my template (I know the attributes don't
> > > > exist; that's what my question is):
>
> > > > {% for choice in form.usertype %}
> > > >     <tr><td>{{ choice.label }}</td><td>{{ choice.field }}</td></tr>
> > > > {% endfor %}
>
> > > > Any ideas? The rest of the form works perfectly so assume all other
> > > > variables are properly defined and are usable.
>
> > > > Thanks!
--~--~---------~--~----~------------~-------~--~----~
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