Skip to content

Instantly share code, notes, and snippets.

@vitorfs
Created September 27, 2017 17:00
Show Gist options
  • Save vitorfs/3c228824067815970ccea66cba27ae18 to your computer and use it in GitHub Desktop.
Save vitorfs/3c228824067815970ccea66cba27ae18 to your computer and use it in GitHub Desktop.
from django import template
register = template.Library()
@register.filter('fieldtype')
def fieldtype(field):
return field.field.widget.__class__.__name__
{% load fieldtype %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
<div class="form-group{% if field.errors %} has-error{% endif %}">
{% if field|fieldtype == 'CheckboxInput' %}
<div class="col-md-10 col-md-offset-2 col-sm-8 col-sm-offset-4">
<div class="checkbox">
<label>
{{ field }} {{ field.label }}
</label>
</div>
{% for error in field.errors %}
<p class="help-block">{{ error }}</p>
{% endfor %}
<span class="help-block">{{ field.help_text }}</span>
</div>
{% else %}
<label for="{{ field.id_for_label }}" class="col-md-2 col-sm-4 control-label">{{ field.label }}</label>
<div class="col-md-10 col-sm-8">
{{ field }}
{% for error in field.errors %}
<p class="help-block">{{ error }}</p>
{% endfor %}
{% if field.help_text %}
<span class="help-block">{{ field.help_text }}</span>
{% endif %}
</div>
{% endif %}
</div>
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment