Loop over a form's choices (radio button) and render it

1,625 views
Skip to first unread message

Andreas Pfrengle

unread,
Aug 7, 2010, 5:03:36 PM8/7/10
to Django users
Hello,

what I want to do is to add a radio button into the last column of a
data table. The user shall be able to select exactly one of the
table's rows. I was thinking about defining a SelectForm with one
'select'-field (ChoiceField) with a RadioSelect-widget. In the
template, I would then need to iterate over the data (each data row)
AND each OPTION of the radio select widget.
How would I do that? Just referring to
{{ selectform.fields.selectfield.choices }} in the template gives me
the list of choices (i.e. each single choice when I loop over it), but
not the html I want.

Are there any other approaches to the problem?

Thanks for your suggestions,
Andreas

Andreas Pfrengle

unread,
Aug 7, 2010, 5:23:20 PM8/7/10
to Django users
Thinking about it, instead of the radio button it would be even better
if I could select just the whole datarow. But I have no idea how to do
it :-?

Andreas Pfrengle

unread,
Aug 11, 2010, 5:21:05 PM8/11/10
to Django users
Just to push it up again... any ideas?

Bill Freeman

unread,
Aug 11, 2010, 6:45:46 PM8/11/10
to django...@googlegroups.com
Non-trivial. I would up writing a template filter to do this. You
have to use undocumented,
I believe, interfaces. I called my filter checkboxiterator, so usage
looks something like:

{% for pseudocheckbox in some_multi_select_field|checkboxiterator %}

It iterates over a set of instances of a non-model class having
attributes you need to render
the checkbox (name, a suitably escaped option_label, option_value,
final_attrs, and a boolean
as to whether optionvlaue is in the bound field's values - that is, is
selected).

It uses another private class that has an __init__ and a render, an
instance of which is
passed to bound_field.as_widget() to capture teh name, data (values)
and attrs. If there's
an 'id' attr, you also have to dumy up an enhanced version for each checkbox.

About 80 lines of code, if I can count the blank lines for prettyness.

Bill

On Wed, Aug 11, 2010 at 5:21 PM, Andreas Pfrengle <a.pfr...@gmail.com> wrote:
> Just to push it up again... any ideas?
>

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

Andreas Pfrengle

unread,
Aug 14, 2010, 7:25:23 AM8/14/10
to Django users
Hello Bill,

thanks for your answer. However, I've never written a template filter
yet. Would you present the code of your checkboxiterator? Is there
sth. on djangosnippets?

Andreas

Bill Freeman

unread,
Aug 16, 2010, 4:50:46 PM8/16/10
to django...@googlegroups.com
Ok. I have permission from my boss, and have cleaned it up a bit. See:

http://djangosnippets.org/snippets/2151/

Bill

Andreas Pfrengle

unread,
Aug 22, 2010, 9:07:12 AM8/22/10
to Django users
Hello Bill,

thanks for the code. It took half the weekend, but finally I built
upon this to get a radiobutton-iterator. This was a bit more
complicated, since the RadioInput widget has no own render-method, so
I needed to introduce a helper class that derives from RadioInput.
I've put the code here:
<http://djangosnippets.org/snippets/2159/>

Would you mind testing it in your app to see if it also still behaves
like it should (the filter that should return the same as your
original version is called "checkboxiterator_named")?
Btw: Your version seems also to work fine on Django 1.2.1.

Andreas

Bill Freeman

unread,
Aug 23, 2010, 10:35:55 AM8/23/10
to django...@googlegroups.com
Andreas,

I'll give it a try, but it won't be soon. Other projects are hot.

Bill

pravasi

unread,
Aug 24, 2010, 4:30:54 PM8/24/10
to Django users
If you have a function as below in the SelectForm class you can get a
iterable renderer for the radio button field.I am not very sure about
whether you have a initial value or value in your situation.

def get_selectfield_renderer(self):
field = self.fields['selectfield']
widget = field.widget
value = self.initial['selectfield']
renderer = widget.get_renderer('selectfield', value)
return renderer

You can then render each radio button choice in the template by using
{{ form.get_selectfield_renderer.0 }} ,
{{ form.get_selectfield_renderer.1 }} etc.If you are using a for loop
you can use the forloop.counter0
Reply all
Reply to author
Forward
0 new messages