有没有办法隐藏csrf标签,而通过使用Flask和Flask-WTForms形式循环? [英] Is there a way to hide the csrf label while looping through form using Flask and Flask-WTForms?

查看:435
本文介绍了有没有办法隐藏csrf标签,而通过使用Flask和Flask-WTForms形式循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有非常简单的联系表单,我想以某种方式隐藏标签,以便它不显示 Csrf标记

  {%for field in form%} $ b我使用了Flask和Flask-WTForms,并且像这样渲染了表单:

$ b {{field.label}}
{{field}}
{%endfor%}

所以基本上这显示我的输入正确,csrf oen隐藏,但标签不隐藏?我应该克服它,并隐式地说 form.field_name 而不是循环通过窗体,或者有办法处理这个角落案例。

我正在考虑在for循环声明或标签声明中进行逻辑检查,但到目前为止,我还没有在文档中找到任何有效的工作。



谢谢

编辑:我通过这样做修复了这个问题,但是感觉有点肮脏和不安,我不喜欢我仍然开放给更好的解决方案:

$ $ $ $ $ $ $ $ {$ if if not loop.first%}
{{field.label} }
{%endif%}


解决方案

你需要一个更通用的解决方案,适用于所有隐藏的字段,而不仅仅是CSRF标记:
$ b

  {{form.hidden_​​tag()如果field.widget.input_type!='hidden'%} 
{{field.label}}
{{field}}
{ %endfor%}


form.hidden_​​tag()由Flask-WTF提供。


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 rendering the form like this:

{% for field in form %}
    {{ field.label }}
    {{ field }}
{% endfor %}

So basically this shows my inputs correctly and the csrf oen is hidden but the label isn't hidden? Should I get over it and implicitly say form.field_name instead of looping through the form or is there a way to handle this "corner case".

I was thinking about doing a logical check in either the for loop declaration or the label declaration but so far I haven't found anything in the documentation that has worked.

Thanks

EDIT: I have "fixed" the problem by doing this but it feels kinda dirty and hacky which I don't like I am still open to a better solution:

{% if not loop.first %}
    {{ field.label }}
{% endif %}

解决方案

If you want a more general solution that works for all hidden fields instead of just the CSRF token:

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

form.hidden_tag() is supplied by Flask-WTF.

这篇关于有没有办法隐藏csrf标签,而通过使用Flask和Flask-WTForms形式循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆