问题
I am following this tutorial http://flask.pocoo.org/docs/0.10/patterns/wtforms/
Here’s an example _formhelpers.html template with a macro:
{% 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 %}
Here the register.html template which takes advantage of the _formhelpers.html template:
{% 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>
It ends up generating a sequence of those:
<dt><label for="email">Email Address</label>
<dd><input id="email" name="email" type="text" value="">
I want to put more parameters and in a bit different fashion like:
<input type="email" id="email" name="email" class="input-xlarge"
placeholder="Email Address" value="{{ form.email }}"
required>
How do I modify
<dt>{{ field.label }}
<dd>{{ field(**kwargs)|safe }}
to achieve that?
回答1:
Coming from the documentation you can do:
{{ form.email(class_="input-xlarge",
placeholder="Email Address",
value="testemail@testing.com") }}
Which will render:
<input type="email" id="email" name="email" class="input-xlarge"
placeholder="Email Address" value="testemail@testing.com"
required>
Just replace "testemail@testing.com" with form.email without braces as you are already inside them.
来源:https://stackoverflow.com/questions/35768370/jinja-flask-and-wtforms-how-to-pass-parameters-in-field