forms.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 captcha(form, width=9) -%}
  43. {% if 'recaptcha' in form.fields %}
  44. {{ row(form.recaptcha) }}
  45. {% endif %}
  46. {% if 'captcha_qa' in form.fields %}
  47. {{ row(form.captcha_qa, width=width) }}
  48. {% endif %}
  49. {%- endmacro %}
  50. {% macro field(_field, attrs=None, width=9) -%}
  51. {% set widget = _field.field.widget.__class__.__name__ %}
  52. {% set context = _field.field.widget.get_context(_field.html_name, _field.value(), attrs=attrs) %}
  53. {% if not 'class' in context['attrs'] and not widget == 'CheckboxInput' %}
  54. {% do context['attrs'].update({'class': ('span' ~ width)}) %}
  55. {% endif %}
  56. {% if 'inline' in context.attrs %}{% do context.attrs.pop('inline') %}{% endif %}
  57. {% if widget == 'Textarea' %}
  58. {{ _textarea(_field, context) }}
  59. {% elif widget == 'Select' %}
  60. {{ _select(_field, context) }}
  61. {% elif widget == 'ReCaptchaWidget' %}
  62. {{ _field.field.widget.render()|safe }}
  63. {% else %}
  64. {{ _input(_field, context) }}
  65. {% endif %}
  66. {%- endmacro %}
  67. {% macro attributes(attrs) -%}
  68. {% for name, value in attrs.items() %} {{ name }}{% if value != True %}="{{ value }}"{% endif %}{% endfor %}
  69. {%- endmacro %}
  70. {% macro _input(_field, context) -%}
  71. <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 %}>
  72. {%- endmacro %}
  73. {% macro _textarea(_field, context) -%}
  74. <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>
  75. {%- endmacro %}
  76. {% macro _select(_field, context) -%}
  77. <select id="id_{{ context.name }}" name="{{ context.name }}" {{ attributes(context.attrs)|trim }}>
  78. {% if context['optgroups']|length > 1 %}
  79. {% for optgroup in context['optgroups'] %}
  80. <optgroup label="{{ optgroup[0] }}">
  81. {% for option in optgroup[1] %}
  82. <option value="{{ option[0] }}"{% if 'value' in context and option[0] in context.value %} selected="selected"{% endif %}>{{ option[1] }}</option>
  83. {% endfor %}
  84. </optgroup>
  85. {% endfor %}
  86. {% else %}
  87. {% for option in context['optgroups'][0][1] %}
  88. <option value="{{ option[0] }}"{% if 'value' in context and option[0] in context.value %} selected="selected"{% endif %}>{{ option[1] }}</option>
  89. {% endfor %}
  90. {% endif %}
  91. </select>
  92. {%- endmacro %}