forms.html 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. {# Forms macros for rendering forms and fields and stuff in templates. #}
  2. {% macro hidden_fields(form) -%}
  3. <input type="hidden" name="{{ csrf_id }}" value="{{ csrf_token }}">
  4. {% for field in form.hidden_fields() %}
  5. <input type="hidden" name="{{ field.html_name }}" value="{{ field.value() }}">
  6. {% endfor %}
  7. {%- endmacro %}
  8. {% macro row(_field, label=None, help_text=None, width=9, attrs=None) -%}
  9. <div id="{{ field.html_name }}-control-group" class="control-group{% if _field.errors %} error{% endif %}">
  10. <label class="control-label" for="id_{{ field.html_name }}">{% if label %}{{ label }}{% elif _field.label %}{{ _field.label }}{% else %}{{ _field.html_name }}{% endif %}</label>
  11. <div class="controls">
  12. {% if attrs == None %}{% set attrs = {} %}{% endif %}
  13. {% if _field.field.widget.__class__.__name__ == 'CheckboxInput' %}
  14. <label class="checkbox">
  15. {{ field(_field, width=width, attrs=attrs)|trim }}
  16. {% if 'inline' in attrs %}
  17. {{ attrs.inline }}
  18. {% else %}
  19. {% if help_text %}
  20. {{ help_text }}
  21. {% elif _field.help_text %}
  22. {{ _field.help_text }}
  23. {% endif %}
  24. {% endif %}
  25. </label>
  26. {% else %}
  27. {{ field(_field, width=width, attrs=attrs)|trim }}
  28. {% endif %}
  29. {% for error in _field.errors %}
  30. <p class="help-block" style="font-weight: bold;">{{ error }}</p>
  31. {% endfor %}
  32. {% if 'inline' in attrs or _field.field.widget.__class__.__name__ != 'CheckboxInput' %}
  33. {% if help_text %}
  34. <p class="help-block">{{ help_text }}</p>
  35. {% elif _field.help_text %}
  36. <p class="help-block">{{ _field.help_text }}</p>
  37. {% endif %}
  38. {% endif %}
  39. </div>
  40. </div>
  41. {%- endmacro %}
  42. {% macro field(_field, attrs=None, width=9) -%}
  43. {% set widget = _field.field.widget.__class__.__name__ %}
  44. {% set context = _field.field.widget.get_context(_field.html_name, _field.value(), attrs=attrs) %}
  45. {% if not 'class' in context['attrs'] and not widget == 'CheckboxInput' %}
  46. {% do context['attrs'].update({'class': ('span' ~ width)}) %}
  47. {% endif %}
  48. {% if 'inline' in context.attrs %}{% do context.attrs.pop('inline') %}{% endif %}
  49. {% if widget == 'Textarea' %}
  50. {{ _textarea(_field, context) }}
  51. {% elif widget == 'Select' %}
  52. {{ _select(_field, context) }}
  53. {% else %}
  54. {{ _input(_field, context) }}
  55. {% endif %}
  56. {%- endmacro %}
  57. {% macro attributes(attrs) -%}
  58. {% for name, value in attrs.items() %} {{ name }}{% if value != True %}="{{ value }}"{% endif %}{% endfor %}
  59. {%- endmacro %}
  60. {% macro _input(_field, context) -%}
  61. <input type="{{ context.type }}" id="id_{{ context.name }}" name="{{ context.name }}" {{ attributes(context.attrs)|trim }}{% if 'value' in context and context.value|length > 0 %} value="{{ context.value }}"{% endif %}>
  62. {%- endmacro %}
  63. {% macro _textarea(_field, context) -%}
  64. <textarea id="id_{{ context.name }}" name="{{ context.name }}" {{ attributes(context.attrs)|trim }}>{% if 'value' in context and context.value|length > 0 %}{{ context.value }}{% endif %}</textarea>
  65. {%- endmacro %}
  66. {% macro _select(_field, context) -%}
  67. <select id="id_{{ context.name }}" name="{{ context.name }}" {{ attributes(context.attrs)|trim }}>
  68. {% if context['optgroups']|length > 1 %}
  69. {% for optgroup in context['optgroups'] %}
  70. <optgroup label="{{ optgroup[0] }}">
  71. {% for option in optgroup[1] %}
  72. <option value="{{ option[0] }}"{% if 'value' in context and option[0] in context.value %} selected="selected"{% endif %}>{{ option[1] }}</option>
  73. {% endfor %}
  74. </optgroup>
  75. {% endfor %}
  76. {% else %}
  77. {% for option in context['optgroups'][0][1] %}
  78. <option value="{{ option[0] }}"{% if 'value' in context and option[0] in context.value %} selected="selected"{% endif %}>{{ option[1] }}</option>
  79. {% endfor %}
  80. {% endif %}
  81. </select>
  82. {%- endmacro %}