将WTForms字段标签与字符串进行比较始终返回False

时间:2015-08-10 18:35:06

标签: python flask wtforms flask-wtforms

如果字段标签是&#34;登录&#34;我不想仅仅根据输入本身显示标签。但是将宏中的field.label与&#34;登录&#34;进行比较始终为false,因为<p>hello</p>永远不会呈现。如何将标签与字符串进行比较?

{% macro render_field(field) %}
  {%if field.label == "Log-in" %}
    <p>hello</p>
  {%else%}
    {{ field.label }} {{ field(**kwargs)|safe }}
  {%endif%}
{% endmacro %}

<div id = "SignUpForm">
    <form action = "" method = "post" name = "SignUpForm">
        <p>{{render_field(form.username)}}</p>
        <p>{{render_field(form.first_name)}}</p>
        <p>{{render_field(form.last_name)}}</p>
        <p>{{render_field(form.password)}}</p>
        <p>{{render_field(form.password2)}}</p>
        <p>{{form.submit}}</p>
        {{form.hidden_tag()}}
    </form>
</div>

1 个答案:

答案 0 :(得分:5)

您需要与field.label.text进行比较。缺少.text部分。标签对象本身不是字符串。