Skip to content

Instantly share code, notes, and snippets.

@LowerDeez
Created August 29, 2017 12:57
Show Gist options
  • Save LowerDeez/647d58883d7699737ec1319d69c40369 to your computer and use it in GitHub Desktop.
Save LowerDeez/647d58883d7699737ec1319d69c40369 to your computer and use it in GitHub Desktop.
Django-Jinja2. Render form in template
{% for hidden_field in form.hidden_fields() %}
{{ hidden_field }}
{% endfor %}
{% if form.non_field_errors %}
{% for error in form.non_field_errors() %}
{{ error }}
{% endfor %}
{% endif %}
{% for field in form.visible_fields() %}
<div class="form-group">
<label for="{{field.id_for_label}}">{{ field.label }}:</label>
{% if field.errors %}
{% for error in field.errors %}
<div class="alert alert-danger" role="alert">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
<span class="sr-only">{% trans %}Error{% endtrans %}:</span>
{{ error }}
</div>
{% endfor %}
{% endif %}
{{ field.as_widget(attrs={'class':'form-control'}) }}
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text|safe }}</small>
{% endif %}
</div>
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment