Wednesday, October 26, 2011

Re: Field.Choices in Template

Hey Guys,

I hate to bother you again. But -- is there a way to get the currently selected data from this set? For example, a user hits the page and all choices are displayed fine. But, they pick some choices. They haven't finished editing the form or maybe they made a mistake, anyways they land in the form_invalid land. I'd like to be able to use this same technique to make sure I properly display what the user has selected thus-far.

Thanks!

On Tue, Oct 25, 2011 at 8:05 AM, Andre Terra <andreterra@gmail.com> wrote:
I remember trying to help you with this on IRC, Kurtis. I've had the
same problem before, but I could never remember that BoundFields have
a field attribute.. If there isn't a note in the docs (or even better,
an example), I'll make sure to write a patch in the next few days.


Cheers,
AT

On 10/25/11, Kurtis <kurtis.mullins@gmail.com> wrote:
> Thank you so much for this, Tom! I've asked this question quite a few
> times in IRC and couldn't come across the answer I was looking for. I
> figured it was possible, I just had no idea how to access it. Much
> appreciation!
>
> On Oct 24, 11:26 am, Tom Evans <tevans...@googlemail.com> wrote:
>> 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+unsubscribe@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

--
Sent from my mobile device

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment