Use Django's standard field rendering. Create filter for custom attrs.

This commit is contained in:
Will Stott
2019-07-17 13:51:53 +01:00
committed by Isaac Bythewood
parent e216e5a6f2
commit a6569cb8ff
2 changed files with 12 additions and 10 deletions

View File

@@ -1,10 +1,10 @@
from django.template import loader, Context, Library
from django.template import Library
register = Library()
@register.simple_tag
def bootstrap_field(field):
template = loader.get_template('core/templatetags/bootstrap_field.html')
return template.render(Context({'field': field}))
@register.filter(name='bootstrap_field')
def bootstrap_field(field, class_attr):
return field.as_widget(attrs={
'placeholder': field.label,
'class': class_attr
})

View File

@@ -1,5 +1,7 @@
{% load bootstrap_field %}
<div class="form-group{% if field.errors %} has-error{% endif %}">
<input type="{% if field.html_name == "password" %}password{% else %}text{% endif %}" id="id_{{ field.html_name }}" name="{{ field.html_name }}" class="form-control input-lg" placeholder="{{ field.label }}"/>
{{ field|bootstrap_field:"form-control input-lg" }}
{% if field.errors %}
<span class="help-block">
{% if field.errors %}