Mar 29 2012

Django template tags to find out field type

published by Iurii Garmash
in blog Programmer blog
original entryDjango template tags to find out field type

approach css django field filter form name python simpletag styling tag templates templatetag type


Sometimes you have the task to decorate django form fields. In our case it was need to add different widget to form field depending on field type.
You can know your field type by refering to it's python class name, e.g.:
field.__class__.__name__


You will get wrong symbols error if you try to refer this python variable in templates directly. So you need a custom template tag to serve here. Decision was not to return variable for tag but to add context variable, cause I had need to perform exact actions on certain field types.

So tag example:
@register.simple_tag(takes_context=True)
def set_this_field_type(context, field):
    """
    Adds to context given field type variable
    variable named "this_field_type"
    """
    context["this_field_type"] = field.field.__class__.__name__
    return ''
Now you can add this variable to your if statements. E.g. usage:
{% load my_template_lib %}
{% for field in form %}
    {# Setting this field type in context #}
    {% set_this_field_type field %}
{% if not this_field_type = "DateField" %}
    {# Rendering specific for Date fields (With DatePicker widget) #}
    <div class="field-wrapper {{ this_field_type }}"
        <label class="control-label">{{ field.label }}</label>
        <input id="{{ field.id_for_label }}" class="datepicker-widget" type="text" name="{{ field.name }}">
    </div>
{% else %}
    {# Default field rendering #}
    <div class="field-wrapper {{ this_field_type }}"
         <label class="control-label">{{ field.label }}</label>
        {{ field }}
    </div>
{% endif %}
{% endfor %}
You will get your form fields rendered in custom manner. You will have the CSS class with field type for styling or you can use this variable in context to render various widgets for e.g.
Hope you've got the main concept here. Now this idea must be limited only by your imagination.

Including those "simple" approaches kindly suggested by guys at 'reddit.com' They are more simple and can play in your case maybe ;)
Using the filter: (Thanks to POTUS @reddit.com)

@register.filter()
def field_type(field):
    return field.field.__class__.__name__

{% if not field|field_type ="DateField" %}
    <span>Whatever</span>
{%endif%}


Using the widget: (Thanks to tgerdes @reddit.com)

from django import forms
class DateForm(forms.Form):
    date = forms.DateField(
      widget=forms.DateInput(attrs= {'class': 'datepicker-widget' } ))

form = DateForm()
print form.as_p()

outputs: <p><label for="id_date">Date:</label> <input id="id_date" type="text" class="datepicker-widget" name="date" /></p>

However this approach in not this topics main cause it concludes we only have to style input. But in some cases it may be quite enough...

Thanks guys for correction!

Please drop me a comment here if you've liked/used this decision(s).


Tag cloud


Social Sharing

Feeds