templates: add support for custom widget template (#54642)

This commit is contained in:
Serghei Mihai 2021-08-13 12:13:03 +02:00
parent 1fa018a2a8
commit 2a6ba2ca06
3 changed files with 13 additions and 4 deletions

View File

@ -17,10 +17,10 @@
{% endfor %}
</div>
{% endif %}
{% for field in form %}
{% for field, widget_template in fields_with_templates %}
{% if field.is_hidden %}
{{ field }}
{% else %}
{% include "gadjo/widget.html" with field=field %}
{% include widget_template with field=field %}
{% endif %}
{% endfor %}

View File

@ -16,7 +16,7 @@
</div>
{% endblock %}
{% block widget-content %}
<div class="content" aria-labelledby="{{ field.name }}">
<div class="content" aria-labelledby="{{ field.name }}" {% block widget-attrs %}{% endblock %}>
{% block widget-error %}
{% if field.errors %}
<div class="error">

View File

@ -122,7 +122,16 @@ def querystring(parser, token):
@register.filter
def with_template(form):
form_template = template.loader.get_template('gadjo/form.html')
return form_template.render({'form': form})
fields_with_templates = []
for field in form:
templates = ['gadjo/%s-widget.html' % field.field.widget.input_type, 'gadjo/widget.html']
fields_with_templates.append(
(
field,
template.loader.select_template(templates),
)
)
return form_template.render({'form': form, 'fields_with_templates': fields_with_templates})
# pattern to transform Django camel case class names to CSS class names with