tags:

views:

17

answers:

2

I've got a field defined like this

country = ChoiceField(initial='CA', choices=COUNTRIES, widget=Select(attrs={'class':'address country'}))

Notice how attrs is set. How can I retrieve this inside the template?

I'm trying things like

{{country.widget.attrs.class}}

But nothing seems to be working.

+2  A: 

I'm assuming you're defining a field within a Django form class, so to access the 'class' attribute of the 'country' field in your template you need to a) prefix the form object name to your variable, and b) throw a 'field' into the mix after your field name, like so:

{{ form_obj.country.field.widget.attrs.class }}
Chris Forrette
I've got got a partial template that I pass just the `field` off to, so I don't really need the `form_obj.` bit... basically it translates to `{{field.field.widget.attrs.class}}` in my code... gotta love that :)
Mark
+1  A: 

over .field you can access the attrs {{ country.field.widget.attrs.class }}

Ashok
+1 but you're a little late :D
Mark