views:

38

answers:

2

I've created a custom template for rendering form fields:

<tr class="{{field.field.widget.attrs.class}}">
    <th class="label">
        <label for="{{field.auto_id}}">
            {{field.label}}
            {% if not field.field.required %}<span class="optional">(optional)</span>{% endif %}
        </label>
    </th>
    <td class="field">
        {{field}}
        {% if field.errors %}<label class="error" for="{{field.auto_id}}">{{field.errors.0}}</label>{% endif %}
        {% if field.help_text %}<small class="help-text">{{field.help_text}}</small>{% endif %}
    </td>
</tr>

But I want to check if the widget is a checkbox, and if so, render it differently. How can I do that in the template?

+1  A: 

{{ field.field.widget.input_type }} will get you this info for a lot of widgets, but not all. I'm not sure if it'll work for the default checkbox widget or not. Worth a shot.

Jeff Croft
Don't think CheckboxInput has this attribute... it does however appear to have a `check_test` attribute... I don't know if it's unique to the checkbox input...but it's working for now... bit of a hack though.
Mark
+1  A: 

Why don't you directly check the class of the field e.g. test if field.__class__ is BooleanField ?

Edit: I am away from my computer so can't try out things, but for now you can just add a flag to your BooleanFields e.g. self.mycheckboxfield = True, and check that in template

Anurag Uniyal
`Variables and attributes may not begin with underscores` Template doesn't let me check that attribute.
Mark
@Mark, see edit
Anurag Uniyal
And put that where exactly? In `myform.__init__`? For every single one of my forms and checkbox fields?
Mark
You can have a custom field, or you can have a custom form rendering or you can have a template tag which does it or you make a custom render_to_response which modifies all forms on the fly
Anurag Uniyal