misc: add django widget type as css class (#46440)

This commit is contained in:
Frédéric Péters 2020-09-08 10:21:47 +02:00
parent f475bc7c3f
commit e72dc51a74
2 changed files with 15 additions and 1 deletions

View File

@ -1,6 +1,7 @@
{% load i18n %}
{% load gadjo i18n %}
<div class="widget
{{ field.css_classes }}
django-{{ field|field_class_name }}
{% if field.errors %}widget-with-error{% endif %}
{% if field.field.required %}widget-required{% else %}widget-optional{% endif %}"
id="{{field.id_for_label}}_p">

View File

@ -7,6 +7,7 @@ from xstatic.main import XStatic
from django import template
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.forms import BoundField
from django.utils.html import escape
from django.utils.http import urlencode
@ -122,3 +123,15 @@ def querystring(parser, token):
def with_template(form):
form_template = template.loader.get_template('gadjo/form.html')
return form_template.render({'form': form})
# pattern to transform Django camel case class names to CSS class names with
# dashes. (CheckboxInput -> checkbox-input)
class_name_pattern = re.compile(r'(?<!^)(?=[A-Z])')
@register.filter
def field_class_name(field):
if isinstance(field, BoundField):
field = field.field
return class_name_pattern.sub('-', field.widget.__class__.__name__).lower()