5

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?

Ivegotaquestion
  • 187
  • 1
  • 9

1 Answers1

4

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.

Jase Rieger
  • 611
  • 3
  • 8
  • Thanks but it did not work: File "/app/templates/register.html", line 53, in block "content" {{ render_field(form.email(class_="input-xlarge", placeholder="Email Address", value="testemail@testing.com")) }} File "/app/templates/_formhelpers.html", line 3, in template
    {{ field(**kwargs)|safe }} AttributeError: 'HTMLString' object has no attribute '__ call __'
    – Ivegotaquestion Mar 03 '16 at 10:48
  • You cant pass it to render_field. You need to modify the code so that the render_field only renders the errors. Unless you are only updating information that can be made generic like class. Then you can modify the render_field to have `
    {{ field(class_="class you want", **kwargs)|safe }}`
    – Jase Rieger Mar 03 '16 at 10:56
  • I think I see what you mean. But how can I add a class to __some__ of fields in _formhelpers.html, not for __all__ of them in _formhelpers.html ? – Ivegotaquestion Mar 03 '16 at 11:44
  • 2
    Have a class parameter for render_field `{% macro render_field(field, class_to_use) %}` and then you can use `{{ field(class_=class_to_use, **kwargs)|safe }}` – Jase Rieger Mar 03 '16 at 11:49
  • How would you pass the required tag in? It's just there, to I suppose you used `InputRequired()`. But how do you pass it in jinja2? – blkpingu Oct 11 '19 at 16:49