Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Determining the type of a form field within a template
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
thsutton@gmail.com  
View profile  
 More options Jun 11, 11:49 pm
From: "thsut...@gmail.com" <thsut...@gmail.com>
Date: Thu, 11 Jun 2009 20:49:42 -0700 (PDT)
Local: Thurs, Jun 11 2009 11:49 pm
Subject: Determining the type of a form field within a template
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jashugan  
View profile  
 More options Jun 12, 12:28 pm
From: Jashugan <jashu...@gmail.com>
Date: Fri, 12 Jun 2009 09:28:09 -0700 (PDT)
Local: Fri, Jun 12 2009 12:28 pm
Subject: Re: Determining the type of a form field within a template
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/#writ...).
Then in your code you do something like:

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Sutton  
View profile  
 More options Jun 14, 1:08 am
From: Thomas Sutton <thsut...@gmail.com>
Date: Sun, 14 Jun 2009 13:08:55 +0800
Local: Sun, Jun 14 2009 1:08 am
Subject: Re: Determining the type of a form field within a template
Hi Jashugan,

On 13/06/2009, at 12:28 AM, Jashugan wrote:

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Gaynor  
View profile  
 More options Jun 14, 1:14 am
From: Alex Gaynor <alex.gay...@gmail.com>
Date: Sun, 14 Jun 2009 00:14:30 -0500
Local: Sun, Jun 14 2009 1:14 am
Subject: Re: Determining the type of a form field within a template

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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google