Is there a way to hide the csrf label while looping through form using Flask and Flask-WTForms?

前端 未结 5 893
隐瞒了意图╮
隐瞒了意图╮ 2020-12-15 16:43

I have very simple contact form and I would like to hide the label somehow so that it doesn\'t show Csrf Token. I am using Flask and Flask-WTForms and am render

5条回答
  •  借酒劲吻你
    2020-12-15 17:10

    Just to add to JD's excellent answer...

    For those stumbling across this question: You can avoid losing the (csrf) hidden field (and thus protection) by adding the condition "if field.widget.input_type!='hidden' " specifically to the label instead of to the form iterator.

    i.e.:

    not

    {{ form.hidden_tag() }}
    {% for field in form if field.widget.input_type != 'hidden' %}
          {{ field.label }}
    {{ field }}
    {% endfor %}
    

    but

    {{ form.hidden_tag() }}
    {% for field in form %}
      {% if field.widget.input_type != 'hidden' %} {{ field.label }} {% endif %}
      {{ field }}
    {% endfor %}
    

提交回复
热议问题