Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja, Flask and WTForms: how to pass parameters in field? [duplicate]

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?

like image 246
Ivegotaquestion Avatar asked Sep 25 '22 04:09

Ivegotaquestion


1 Answers

Coming from the documentation you can do:

{{ form.email(class_="input-xlarge", 
              placeholder="Email Address",
              value="[email protected]") }}

Which will render:

<input type="email" id="email" name="email" class="input-xlarge"
        placeholder="Email Address" value="[email protected]"
        required>

Just replace "[email protected]" with form.email without braces as you are already inside them.

like image 140
Jase Rieger Avatar answered Oct 02 '22 14:10

Jase Rieger