iterating through form.fields

2,306 views
Skip to first unread message

nivhab

unread,
Mar 22, 2009, 4:20:38 AM3/22/09
to Django users
Hi,

I have this problem which is probably right in front of my eyes but I
just cannot get it. Maybe someone can point me in the right direction:

I am trying to iterate through form.fields in a template.
The form is rather simple:

class ProductForm(forms.Form):
name = forms.CharField(max_length=200)
description = forms.CharField(max_length=200)
url = forms.URLField('Product URL Page', verify_exists=True,
required=False)

Iteration in template (standard):

{% for field in form.fields %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}

The result of this code is EMPTY text.
However, the following code which reference specific fields:

{{ form.name.label_tag}}: {{ form.name }}

(and every other field I reference) renders the expected html without
any issue.

Any hints why this is happening?

Thanks, Yaniv

Daniel Roseman

unread,
Mar 22, 2009, 4:44:11 AM3/22/09
to Django users
Don't loop through form.fields - as the documentation shows, you
should loop through form.
http://docs.djangoproject.com/en/dev/topics/forms/#looping-over-the-form-s-fields
--
DR.

Yaniv Haber

unread,
Mar 22, 2009, 5:09:14 AM3/22/09
to Django users
Thanks. It works that way although the examples in the documentation
also show iterating through form.visible_fields which result in
nothing. This is where I came from. Any idea why that happens?


On Mar 22, 10:44 am, Daniel Roseman <roseman.dan...@googlemail.com>
wrote:
> should loop through form.http://docs.djangoproject.com/en/dev/topics/forms/#looping-over-the-f...
> --
> DR.

Karen Tracey

unread,
Mar 22, 2009, 12:41:10 PM3/22/09
to django...@googlegroups.com
On Sun, Mar 22, 2009 at 5:09 AM, Yaniv Haber <yaniv...@gmail.com> wrote:

Thanks. It works that way although the examples in the documentation
also show iterating through form.visible_fields which result in
nothing. This is where I came from. Any idea why that happens?

What version of Django are you using?  The doc notes that visible_fields and hidden_fields are new in development, so they are only going to be available if you are using a recent trunk checkout or the 1.1 alpha release.

Karen
Reply all
Reply to author
Forward
0 new messages