Determining the type of a form field within a template

872 views
Skip to first unread message

thsu...@gmail.com

unread,
Jun 11, 2009, 11:49:42 PM6/11/09
to Django users
Hi all,

I'm currently working on my first Django project and have run into a
difficulty while trying to write a generic "display a form" template
that I can {% include %} when I need to (as suggested in the
documentation <http://docs.djangoproject.com/en/dev/topics/forms/>).

My goal is a simple template which renders each [visible] form field
in a div with an id ("{{ field.auto_id }}_wrapper") and a few classes.
In particular, I need to add a class so that different types of form
field can be styled differently w.r.t. their labels and errors.

I generally accomplish this with markup like:

<div id="id_tos_wrapper" class="form-field form-checkbox">
<label for="id_tos">I agree to the terms of service.</label>
<input name="tos" type="checkbox" id="id_tos" />
</div>

I've been trying to find out how to accomplish this in Django
templates, but the closest I've been able to find are two threads on
this list <http://groups.google.com/group/django-users/browse_thread/
thread/d5368f47ff247674> and <http://groups.google.com/group/django-
users/browse_thread/thread/a2aa180890fdd4de> which both seem to
indicate that it is impossible at the template level to get any
information about a form field beyond its auto_id, errors, and the
HTML for the label, input, etc.

This has been trivial in every other framework I've used and I'm sure
it is in Django as well, but I'm completely stumped. Is there any way
to do this which doesn't require that I write custom code for every
form and/or checkbox in my project?

Cheers,

Thomas Sutton

Jashugan

unread,
Jun 12, 2009, 12:28:09 PM6/12/09
to Django users
On Jun 11, 8:49 pm, "thsut...@gmail.com" <thsut...@gmail.com> wrote:

>
> This has been trivial in every other framework I've used and I'm sure
> it is in Django as well, but I'm completely stumped. Is there any way
> to do this which doesn't require that I write custom code for every
> form and/or checkbox in my project?

You may be able to write a custom filter that takes in a field, and
returns the type of field it is (see
http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters).
Then in your code you do something like:

{% ifequal field|field_type 'checkbox' %}
...
{% endifequal %}

Thomas Sutton

unread,
Jun 14, 2009, 1:08:55 AM6/14/09
to django...@googlegroups.com
Hi Jashugan,

That's what I thought, but isn't `field` still an instance of
BoundField? And it still doesn't contain anything that lets me
determine the type of the original field (other than looking at the
generated HTML and taking a guess)?

Cheers,

Thomas Sutton

Alex Gaynor

unread,
Jun 14, 2009, 1:14:30 AM6/14/09
to django...@googlegroups.com
field is a BoundField but field.field is the orignal field object

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero
Reply all
Reply to author
Forward
0 new messages