Commit e34ec60f authored by Bryan  BRANCOTTE's avatar Bryan BRANCOTTE
Browse files

bootstrap handle checkbox input as bootstrap wants to

parent d835f7f2
import logging
from django import template
from django.forms import BoundField
from django.forms import BoundField, widgets
from django.utils.safestring import mark_safe
register = template.Library()
......@@ -17,17 +17,37 @@ def url_replace(request, field, value):
@register.filter
def bootstrap(object):
ret = []
if isinstance(object, BoundField):
field = object
attrs = {"class": "form-control" + (" is-invalid" if field.errors else "")}
ret = []
attrs = field.field.widget.attrs
css_classes = set(attrs.get("class", "").split(" "))
if field.errors:
css_classes.add("is-invalid")
print(type(field.field.widget))
if isinstance(field.field.widget, widgets.CheckboxInput):
wrapping_classes = "form-check"
label_classes = "form-check-label"
css_classes.add("form-check-input")
else:
wrapping_classes = "input_field form-group"
label_classes = "form-control-placeholder"
css_classes.add("form-control")
attrs["class"] = " ".join(css_classes)
ret.append('<div class="%s">' % wrapping_classes)
ret.append(field.as_widget(attrs=attrs))
ret.append("""<label class="form-control-placeholder">%s</label>""" % field.label)
ret.append("""<label class="%s">%s</label>""" % (label_classes, field.label))
if field.errors:
for e in field.errors:
ret.append("""<div class="invalid-feedback">%s</div>""" % e)
ret.append('</div>')
return mark_safe("".join(ret))
else:
logger.error("Can't bootstrapize object of class %s" % str(type(object)))
return object
@register.filter('startswith')
def startswith(text, starts):
return bootstrap(text.startswith(starts))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment