From 02e432ca21d3e12f1933257676073eb871c83342 Mon Sep 17 00:00:00 2001 From: Ilona Podliashanyk Date: Mon, 14 Oct 2024 15:26:06 +0200 Subject: [PATCH 1/2] Add check to handle empty form attributes in non-crispy form content If no form.attrs are set then the standard form fields will be rendered. Works as a failsafe. --- .../_form_content.html | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/python/nav/web/templates/custom_crispy_templates/_form_content.html b/python/nav/web/templates/custom_crispy_templates/_form_content.html index aa163528d2..c631645d99 100644 --- a/python/nav/web/templates/custom_crispy_templates/_form_content.html +++ b/python/nav/web/templates/custom_crispy_templates/_form_content.html @@ -1,21 +1,25 @@ {# NB! This template can be used directly (without form template wrapper) for cases where form.helper.form_tag is set to False. #} {% load forms %} -{% if form.attrs.method|lower == 'post' %} - {% csrf_token %} -{% endif %} +{% if form.attrs %} + {% if form.attrs.method|lower == 'post' %} + {% csrf_token %} + {% endif %} -{% include 'foundation-5/errors.html' %} + {% include 'foundation-5/errors.html' %} -{% block form_fields %} - {% if form.attrs.form_fields %} - {% include 'custom_crispy_templates/_form_fields.html' with fields=form.attrs.form_fields %} - {% else %} - {% for field in form %} - {% show_field field %} - {% endfor %} - {% endif %} - {% if form.attrs.submit_field %} - {% include 'custom_crispy_templates/submit_field.html' with input=form.attrs.submit_field %} - {% endif %} -{% endblock form_fields %} + {% block form_fields %} + {% if form.attrs.form_fields %} + {% include 'custom_crispy_templates/_form_fields.html' with fields=form.attrs.form_fields %} + {% else %} + {% for field in form %} + {% show_field field %} + {% endfor %} + {% endif %} + {% if form.attrs.submit_field %} + {% include 'custom_crispy_templates/submit_field.html' with input=form.attrs.submit_field %} + {% endif %} + {% endblock form_fields %} +{% else %} + {{ form }} +{% endif %} From 626cc3e873dc301a88ea98ed0440fb82cb330743 Mon Sep 17 00:00:00 2001 From: Ilona Podliashanyk Date: Mon, 4 Nov 2024 14:51:29 +0100 Subject: [PATCH 2/2] Add fallback for uncrispified forms where form.attrs were not set It is not necessarily a complete and/or appropriate fallback for forms that are originally non-crispy. This is in ulikely unforeseen cases where for some reason Django widget can not be rendered as a foundation-5/field.html template without loosing some field data. Or a much more likely scenario when styles in foundation-5/field.html are not fitting for the form when reusing this template. To sum it up: it should be generic enough for forms that are originally non-crispy but it is not a given. So if this template is to be reused by non-crispy forms (in contrast to uncrispified forms), the rendering of forms should be properly tested. --- .../web/templates/custom_crispy_templates/_form_content.html | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python/nav/web/templates/custom_crispy_templates/_form_content.html b/python/nav/web/templates/custom_crispy_templates/_form_content.html index c631645d99..3cc15fdd52 100644 --- a/python/nav/web/templates/custom_crispy_templates/_form_content.html +++ b/python/nav/web/templates/custom_crispy_templates/_form_content.html @@ -21,5 +21,7 @@ {% endif %} {% endblock form_fields %} {% else %} - {{ form }} + {% for field in form %} + {% show_field field %} + {% endfor %} {% endif %}