On Thu, Oct 20, 2011 at 8:37 PM, Kurtis <kurtis.mull...@gmail.com> wrote:
> Hey,
>
> I'm trying to build a custom template for my ModelForm. My ModelForm
> contains several M2M fields. If I run the ModelForm.as_view and then
> in my template print {{ form.as_p }} it'll automagically display those
> choices.
>
> Now what I want to do is take advantage of that magic and print out
> those choices myself in my template. For example:
>
> {% for choice in form.genres.choices %}
>    {{ choice }}
> {% endfor %}
>
> ... but it doesn't work. it doesn't print anything. I tried reading
> through the source code of the forms.py and fields.py from 1.3 but
> didn't get very far.
>

When you access a form field from a template, the field returned is a
BoundField, not the field attribute from the form class. From the
BoundField object, you can access the form field on the field
attribute, eg:

{% for choice in form.genres.field.choices %}
   {{ choice }}
{% endfor %}

Cheers

Tom

-- 
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