_forms.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. {% load i18n %}
  2. {# Render whole form macro #}
  3. {%- macro form_widget(form, horizontal=false, width=12) -%}
  4. <fieldset>
  5. <input type="hidden" name="{{ csrf_id }}" value="{{ csrf_token }}">{% for field in form.hidden %}
  6. <input type="hidden" name="{{ field.id }}" value="{{ field.value }}">{% endfor %}{% for fieldset in form.fieldsets %}{% if fieldset.legend %}
  7. <legend><div>{{ fieldset.legend }}{% if fieldset.help %} <span>{{ fieldset.help }}</span>{% endif %}</div></legend>{% endif %}
  8. {% for field in fieldset.fields %}
  9. {{ row_widget(field, horizontal=horizontal, width=width) }}
  10. {% endfor %}
  11. </fieldset>{% if not fieldset.last %}
  12. <fieldset>{% endif %}{% endfor %}
  13. {%- endmacro -%}
  14. {# Render form row macro #}
  15. {%- macro row_widget(field, horizontal=false, width=12) -%}
  16. <div class="control-group{% if field.errors %} error{% endif %}">{% if field.label %}
  17. <label class="control-label" for="{{ field.html_id }}">{{ field.label }}:</label>{% endif %}{% if field.nested %}
  18. <div class="controls controls-nested">
  19. <div class="row">
  20. {% for subfield in field.nested %}
  21. <div class="span{{ widthratio(subfield.width, 100, width) }}">{{ field_widget(subfield, horizontal=horizontal, width=width, nested=true) }}</div>
  22. {% endfor %}
  23. </div>{% for error in field.errors %}
  24. <p class="help-block" style="font-weight: bold;">{{ error }}</p>{% endfor %}{% if field.widget != "checkbox" and field.help_text %}
  25. <p class="help-block">{{ field.help_text }}</p>{% endif %}
  26. </div>{% else %}
  27. <div class="controls">
  28. {{ field_widget(field, horizontal=horizontal, width=width) }}{% for error in field.errors %}
  29. <p class="help-block" style="font-weight: bold;">{{ error }}</p>{% endfor %}{% if field.widget != "checkbox" and field.help_text %}
  30. <p class="help-block">{{ field.help_text }}</p>{% endif %}
  31. </div>{% endif %}
  32. </div>
  33. {%- endmacro -%}
  34. {# Render form field macro #}
  35. {%- macro field_widget(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  36. {%- if field.widget == "checkbox" -%}
  37. {{ input_checkbox(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  38. {%- endif -%}
  39. {%- if field.widget == "date" -%}
  40. {{ input_date(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  41. {%- endif %}
  42. {%- if field.widget == "recaptcha" -%}
  43. {{ input_recaptcha(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  44. {%- endif -%}
  45. {%- if field.widget == "radio_select" -%}
  46. {{ input_radio_select(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  47. {%- endif -%}
  48. {%- if field.widget == "select" -%}
  49. {{ input_select(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  50. {%- endif -%}
  51. {%- if field.widget == "checkbox_select_multiple" -%}
  52. {{ input_checkbox_select_multiple(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  53. {%- endif -%}
  54. {%- if field.widget == "text" -%}
  55. {{ input_text(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  56. {%- endif -%}
  57. {%- if field.widget == "textarea" -%}
  58. {{ input_textarea(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  59. {%- endif -%}
  60. {%- if field.widget == "yes_no_switch" -%}
  61. {{ input_yes_no_switch(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  62. {%- endif %}
  63. {%- endmacro -%}
  64. {# Render form field attributes macro #}
  65. {%- macro field_attrs(attrs={}, extras=[]) -%}
  66. {% for attribute in attrs %} {{ attribute }}="{{ attrs[attribute] }}"{% endfor %}{% for extra in extras %} {{ extra }}{% endfor %}
  67. {%- endmacro -%}
  68. {# Render form field class attribute macro #}
  69. {%- macro field_classes(classes=[]) -%}
  70. {% if classes %} class="{% for class in classes %}{% if not loop.first %} {% endif %}{{ class }}{% endfor %}"{% endif %}
  71. {%- endmacro -%}
  72. {# Checkbox input #}
  73. {%- macro input_checkbox(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  74. <label class="checkbox">
  75. <input type="checkbox" name="{{ field.html_name }}" id="{{ field.html_id }}" value="1"{% if field.value %} checked="checked"{% endif %}>
  76. {% if field.inline is defined %}{{ field.inline }}{% else %}{{ field.label }}{% endif %}
  77. </label>
  78. {%- endmacro -%}
  79. {# Date input #}
  80. {%- macro input_date(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  81. {%- do field.attrs.update(attrs) -%}
  82. {%- if horizontal -%}
  83. {%- do classes.append('span' ~ (widthratio(field.width, 100, width) - 2)) -%}
  84. {%- else -%}
  85. {%- do classes.append('span' ~ widthratio(field.width, 100, width)) -%}
  86. {%- endif -%}
  87. <input type="text"{{ field_attrs(field.attrs) }}{{ field_classes(classes) }}{% if field.has_value %} value="{{ field.value }}"{% endif %}>
  88. {%- endmacro -%}
  89. {# Multiple Checkbox input #}
  90. {%- macro input_checkbox_select_multiple(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  91. {%- do field.attrs.update(attrs) -%}
  92. {%- do classes.append('select-multiple') -%}
  93. <div{{ field_classes(classes) }}>{% for choice in field.choices %}
  94. <label class="checkbox">
  95. <input type="checkbox" name="{{ field.html_name }}" id="{{ field.html_id }}_{{ choice[0] }}" value="{{ choice[0] }}"{% if field.value and choice[0] in field.value %} checked="checked"{% endif %}>
  96. {{ choice[1] }}
  97. </label>{% endfor %}
  98. </div>
  99. {%- endmacro -%}
  100. {# Recaptcha input #}
  101. {%- macro input_recaptcha(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  102. RECAPTCHA!
  103. {%- endmacro -%}
  104. {# RadioSelect input #}
  105. {%- macro input_radio_select(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  106. {%- do field.attrs.update(attrs) -%}
  107. {%- do classes.append('radio-group') -%}
  108. <div{{ field_classes(classes) }}>{% for choice in field.choices %}
  109. <label class="radio">
  110. <input type="radio" name="{{ field.html_name }}" id="{{ field.html_id }}{{ choice[0] }}" value="{{ choice[0] }}"{% if field.value == choice[0] %} checked="checked"{% endif %}>
  111. {{ choice[1] }}
  112. </label>{% endfor %}
  113. </div>
  114. {%- endmacro -%}
  115. {# Select input #}
  116. {%- macro input_select(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  117. {%- do field.attrs.update(attrs) -%}
  118. {%- if horizontal %}
  119. {%- do classes.append('span' ~ (widthratio(field.width, 100, width) - 2)) -%}
  120. {%- else -%}
  121. {%- do classes.append('span' ~ widthratio(field.width, 100, width)) -%}
  122. {%- endif -%}
  123. <select{{ field_attrs(field.attrs) }}{{ field_classes(classes) }}>{% for choice in field.choices %}
  124. <option value="{{ choice[0] }}"{% if field.value == choice[0] %} selected="selected"{% endif %}>{{ choice[1] }}</option>{% endfor %}
  125. </select>
  126. {%- endmacro -%}
  127. {# Text/password input #}
  128. {%- macro input_text(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  129. {%- do field.attrs.update(attrs) -%}
  130. {%- if horizontal -%}
  131. {%- do classes.append('span' ~ (widthratio(field.width, 100, width) - 2)) -%}
  132. {%- else -%}
  133. {%- do classes.append('span' ~ widthratio(field.width, 100, width)) -%}
  134. {%- endif -%}
  135. <input{{ field_attrs(field.attrs) }}{{ field_classes(classes) }}{% if field.attrs.type != 'password' and field.has_value %} value="{{ field.value }}"{% endif %}>
  136. {%- endmacro -%}
  137. {# Textarea input #}
  138. {%- macro input_textarea(field, attrs={'rows': 4}, classes=[], horizontal=false, width=12, nested=false) -%}
  139. {%- do field.attrs.update(attrs) -%}
  140. {%- if horizontal -%}
  141. {%- do classes.append('span' ~ (widthratio(field.width, 100, width) - 2)) -%}
  142. {%- else -%}
  143. {%- do classes.append('span' ~ widthratio(field.width, 100, width)) -%}
  144. {%- endif -%}
  145. <textarea{{ field_attrs(field.attrs) }}{{ field_classes(classes) }}>{% if field.has_value %}{{ field.value }}{% endif %}</textarea>
  146. {%- endmacro -%}
  147. {# YesNoSwitch input #}
  148. {%- macro input_yes_no_switch(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  149. {%- do field.attrs.update(attrs) -%}
  150. {%- do classes.append('yes-no-switch') -%}
  151. <div{{ field_classes(classes) }}>
  152. <input name="{{ field.html_name }}" id="{{ field.html_id }}" type="checkbox" value="1"{% if field.value %} checked="checked"{% endif %}>
  153. </div>
  154. {%- endmacro -%}