Simplifies and enhance form rendering in Django templates.

pip install djformtags==1.0


Control Django form field rendering from within the template

This app provides template tags that simplify and enhance form rendering in Django templates.

One: Set custom attributes on a form field

Say you have the following form with an email field.

class EmailForm(forms.Form):
    email = forms.CharField(max_length=256)

Now, you want to reuse this form, but optimized for a mobile device. Let's do it in the form class:

class EmailField(forms.TextInput):
    input_type = "email"

class MobileEmailForm(EmailForm):
    email = forms.CharField(
            "autocapitalize": "off",
            "class": "mobile-input",
            "placeholder": "",

Really? That's ugly, it requires a new form definition, and it's messing with stuff that belongs in the template.

Enter {% setattr %}:

{% load formtags %}

{% setattr "type" "email" %}
{% setattr "autocapitalize" "off" %}
{% setattr "class" "mobile-input" %}
{% setattr "placeholder" "" %}
{{ }}

And you get:

<input type="email" name="email" maxlength="256" autocapitalize="off" placeholder="" class="mobile-input" id="id_email" />

Set whatever attribute you want directly on the field right in the template where you're rendering the form. No boilerplate needed.

Special attributes

In addition to setting an attribute, the following settings will modify the field or widget:

  • type
  • label
  • initial
  • classes

Note: Using the classes argument will set the class to the field in addition to the existing field class. Using class will override the field class.

Two: Rendering form rows

This tag is more convenience than anything.

Start with a basic form row template, forms/row.html:

<div class="form-row">
  <div class="formrow-label">
    <label for="{{ field.auto_id }}" class="label {% if field.field.required %}label-required{% else %}label-optional{% endif %}">
      {{ field.label|safe }}
    {% if label_help_text %}<div class="form-row-label-help-text">{{ label_help_text|safe }}</div>{% endif %}
  <div class="formrow-input">
    {% if field.errors %}
      <div class="form-row-error-message"><i class="fa fa-warning"></i> {{ field.errors.0 }}</div>
    {% endif %}

    {{ field }}

Enter {% formrow %}:

{% formrow %}

You can also pass in a couple common arguments. Any extra values are passed directly to the form row template context.

{% formrow class="input-email" label="Email Address" template="formrow.html" foo="bar" %}

The template will also be rendered with an input_type variable in the context. This can be used to customize rendering for different field types.

{% if input_type == "checkbox" %}
  .. handle checkbox
{% elif input_type == "radio" %}
  .. handle radio
{% elif input_type == "text" %}
  .. handle text
{% endif %}

Input type is one of the following:



Install with pip:

pip install djformtags

Or install from github

pip install -e

Add djformtags to INSTALLED_APPS.