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?
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?
> 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?
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)?
On Sun, Jun 14, 2009 at 12:08 AM, Thomas Sutton <thsut...@gmail.com> wrote:
> Hi Jashugan,
> On 13/06/2009, at 12:28 AM, Jashugan wrote:
> > 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
> 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
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