Jinja,Flask和WTForms:如何在字段中传递参数?

时间:2016-03-03 09:40:56

标签: python flask jinja2 wtforms flask-wtforms

我正在关注本教程http://flask.pocoo.org/docs/0.10/patterns/wtforms/

以下是带有宏的示例_formhelpers.html模板:

{% macro render_field(field) %}
  <dt>{{ field.label }}
  <dd>{{ field(**kwargs)|safe }}
  {% if field.errors %}
    <ul class=errors>
    {% for error in field.errors %}
      <li>{{ error }}</li>
    {% endfor %}
    </ul>
  {% endif %}
  </dd>
{% endmacro %}

这里是利用_formhelpers.html模板的register.html模板:

{% from "_formhelpers.html" import render_field %}
<form method=post action="/register">
  <dl>
    {{ render_field(form.username) }}
    {{ render_field(form.email) }}
    {{ render_field(form.password) }}
    {{ render_field(form.confirm) }}
    {{ render_field(form.accept_tos) }}
  </dl>
  <p><input type=submit value=Register>
</form>

它最终产生了一系列:

<dt><label for="email">Email Address</label>
<dd><input id="email" name="email" type="text" value="">

我想以更多的方式添加更多参数,如:

      <input type="email" id="email" name="email" class="input-xlarge"
        placeholder="Email Address" value="{{ form.email }}"
        required>

如何修改

  <dt>{{ field.label }}
  <dd>{{ field(**kwargs)|safe }}

实现这个目标?

1 个答案:

答案 0 :(得分:2)

来自你可以做的documentation

{{ form.email(class_="input-xlarge", 
              placeholder="Email Address",
              value="testemail@testing.com") }}

将呈现:

&#13;
&#13;
<input type="email" id="email" name="email" class="input-xlarge"
        placeholder="Email Address" value="testemail@testing.com"
        required>
&#13;
&#13;
&#13;

只需更换&#34; testemail@testing.com"使用form.email没有大括号,因为你已经在其中。