Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to markup form fields with <div class='field_type'> in Django
    primarykey
    data
    text
    <p>I wasn't able to find a way to identify the type of a field in a django template. My solution was to create a simple filter to access the field and widget class names. I've included the code below in case it's helpful for someone else.</p> <p>Is there a better approach?</p> <pre><code>## agency/tagutils/templatetags/fieldtags.py ############################################################### from django import template register = template.Library() @register.filter(name='field_type') def field_type(value): return value.field.__class__.__name__ @register.filter(name='widget_type') def widget_type(value): return value.field.widget.__class__.__name__ ## client/project/settings.py ############################################################### INSTALLED_APPS = ( # ... 'agency.tagutils', ) ## client/project/templates/project/field_snippet.html ############################################################### {% load fieldtags %} &lt;div class="field {{ field|field_type }} {{ field|widget_type }} {{ field.name }}"&gt; {{ field.errors }} &lt;div class="form_label"&gt; {{ field.label_tag }} &lt;/div&gt; &lt;div class="form_field"&gt; {{ field }} &lt;/div&gt; &lt;/div&gt; ## sample output html ############################################################### &lt;div class="field CharField TextInput family_name"&gt; &lt;div class="form_label"&gt; &lt;label for="id_family_name"&gt;Family name&lt;/label&gt; &lt;/div&gt; &lt;div class="form_field"&gt; &lt;input id="id_family_name" type="text" name="family_name" maxlength="64" /&gt; &lt;/div&gt; &lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload