youyongsong
1/24/2015 - 12:08 PM

This is a django-template snippet for forms.

This is a django-template snippet for forms.

<form role="form" action="." method="post"> {% csrf_token %}
  {% if form.non_field_errors %}
  <div class="alert alert-danger">{{ form.non_field_errors|join:"<br \>" }}</div>
  {% endif %}

  {% for field in form %}
  <div class="form-group {% if field.errors %}has-error{% endif %}">
    {{ field.label_tag|add_class:"control-label" }}
    {% if field.help_text %}
    <span class="help-block">{{ field.help_text }}</span>
    {% endif %}
    {% if field.errors %}
    <span class="help-block">{{ field.errors|join:"<br \>" }}</span>
    {% endif %}
    {{ field|add_class:"form-control" }}
  </div>
  {% endfor %}

  <input class="btn btn-success" type="submit" value="{% trans 'submit' %}">
</form>

required

  • {% load trans%}before using this snippets
  • Add this template filter to your custom templatetags and load it before using this snippets
  • Bootstrap framework