Move form field details into a separate include file
authorMagnus Hagander <magnus@hagander.net>
Wed, 7 Oct 2020 15:08:58 +0000 (17:08 +0200)
committerMagnus Hagander <magnus@hagander.net>
Wed, 7 Oct 2020 16:53:45 +0000 (18:53 +0200)
templates/base/form_contents.html
templates/base/form_field.html [new file with mode: 0644]

index 07a8edb94739efaac18ef89b2d95fa970baac64b..99053f50b5327cce7d1694451a3e49f4c333a65f 100644 (file)
@@ -1,4 +1,3 @@
-{%load pgfilters%}
 <form class="form-horizontal" method="post" action=".">{%if not nocsrf%}{% csrf_token %}{%endif%}
   {%if form.errors %}
     <div class="alert alert-danger">
@@ -6,60 +5,7 @@
     </div>
   {%endif%}
   {% for field in form %}
-    {% if field.is_hidden %}
-      {{ field }}
-    {% else %}{# not field.is_hidden #}
-      {% if not field|ischeckbox %}
-        <div class="form-group">
-          {{field|label_class:"control-label col-lg-12"}}
-          <div class="col-lg-12 controls">
-            {% if field.errors %}
-              {% for e in field.errors %}
-                <div class="alert alert-danger">{{e}}</div>
-              {% endfor %}
-            {% endif %}
-            {{field|field_class:"form-control"}}
-            {%if field.help_text%}
-              <small>{{field.help_text|safe}}</small>
-            {%endif%}
-          </div>
-        </div>
-      {% else %}{# field|ischeckbox #}
-        <div class="form-group">
-          <div class="col-lg-12 {%if field|isrequired_error%} alert alert-danger{%endif%}">
-            {% if not field|ismultiplecheckboxes %}
-              <div class="form-check">
-                {{ field }}
-                <label for="{{ field.id_for_label }}" class="form-check-label">
-                  {{ field.label }}
-                </label>
-                {%if field.help_text%}
-                  <small class="form-text text-muted">{{field.help_text|safe}}</small>
-                {%endif%}
-              </div>
-            {% else %}
-              {{ field.label_tag }}
-             {%if field.help_text%}<small class="form-text">{{field.help_text|safe}}</small>{%endif%}
-              {% for cb in field %}
-                <div class="form-check">
-                  {{ cb.tag }}
-                  <label for="{{ cb.id_for_label }}" class="form-check-label">{{ cb.choice_label }}</label>
-{%if described_checkboxes|dictlookup:field.name%}
-
-                 <div class="describedcheckbox">{{described_checkboxes|dictlookup:field.name|dictlookup:cb.data.value}}</div>
-{%endif%}
-                </div>
-              {% endfor %}
-            {% endif %}
-          </div>
-          {% if field.errors and not field|isrequired_error %}
-            {% for e in field.errors %}
-              <div class="alert alert-danger">{{e}}</div>
-            {% endfor %}
-          {% endif %}
-        </div>
-      {% endif %}
-    {% endif %}
+{%include "base/form_field.html" %}
   {%endfor%}
   <button type="submit" class="btn btn-primary">{{savebutton|default:"Save"}}</button>
 </form>
diff --git a/templates/base/form_field.html b/templates/base/form_field.html
new file mode 100644 (file)
index 0000000..70ce284
--- /dev/null
@@ -0,0 +1,55 @@
+{%load pgfilters%}
+    {% if field.is_hidden %}
+      {{ field }}
+    {% else %}{# not field.is_hidden #}
+      {% if not field|ischeckbox %}
+        <div class="form-group">
+          {{field|label_class:"control-label col-lg-12"}}
+          <div class="col-lg-12 controls">
+            {% if field.errors %}
+              {% for e in field.errors %}
+                <div class="alert alert-danger">{{e}}</div>
+              {% endfor %}
+            {% endif %}
+            {{field|field_class:"form-control"}}
+            {%if field.help_text%}
+              <small>{{field.help_text|safe}}</small>
+            {%endif%}
+          </div>
+        </div>
+      {% else %}{# field|ischeckbox #}
+        <div class="form-group">
+          <div class="col-lg-12 {%if field|isrequired_error%} alert alert-danger{%endif%}">
+            {% if not field|ismultiplecheckboxes %}
+              <div class="form-check">
+                {{ field }}
+                <label for="{{ field.id_for_label }}" class="form-check-label">
+                  {{ field.label }}
+                </label>
+                {%if field.help_text%}
+                  <small class="form-text text-muted">{{field.help_text|safe}}</small>
+                {%endif%}
+              </div>
+            {% else %}
+              {{ field.label_tag }}
+             {%if field.help_text%}<small class="form-text">{{field.help_text|safe}}</small>{%endif%}
+              {% for cb in field %}
+                <div class="form-check">
+                  {{ cb.tag }}
+                  <label for="{{ cb.id_for_label }}" class="form-check-label">{{ cb.choice_label }}</label>
+{%if described_checkboxes|dictlookup:field.name%}
+
+                 <div class="describedcheckbox">{{described_checkboxes|dictlookup:field.name|dictlookup:cb.data.value}}</div>
+{%endif%}
+                </div>
+              {% endfor %}
+            {% endif %}
+          </div>
+          {% if field.errors and not field|isrequired_error %}
+            {% for e in field.errors %}
+              <div class="alert alert-danger">{{e}}</div>
+            {% endfor %}
+          {% endif %}
+        </div>
+      {% endif %}
+    {% endif %}