Django template tags to find out field 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.:

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:
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="{{ }}">
{% else %}
    {# Default field rendering #}
    <div class="field-wrapper {{ this_field_type }}"
         <label class="control-label">{{ field.label }}</label>
        {{ field }}
{% 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 '' They are more simple and can play in your case maybe ;)
Using the filter: (Thanks to POTUS

def field_type(field):
    return field.field.__class__.__name__

{% if not field|field_type ="DateField" %}

Using the widget: (Thanks to tgerdes

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).


  1. Thank you man, I really like this simple approach.

    To make it more complex is there possibility to do the same using 'with' tag?

    1. I think that answer is yes.
      You will get context variable "this_field_type" and can use this in whatever approach you are using. It will always be equal to string representation of field name. BTW... You can change context["this_field_type"] to context["whatever_name_you_like"] and do not use the "with" tag... Only if your logic is simple enough though...
      OR you can make simple tag return something and stick to that. In my case it had to create a variable for iteration usage...


Post a Comment

Popular posts from this blog

Python converting PDF to Image

How to disable/enable an element with jQuery or Javascript

Django: Resetting Passwords (with internal tools)