Use Macro to display the validation error messages

Create a file '_formhelper.html' in templates folder.

{% macro render_field(field) %}
    {{ field(**kwargs)|safe }}
        {% if field.errors %}
            {% for error in field.errors %}
                <span class="help-block" style="text-align:left; font-weight:bold">{{ error }}</span>
            {% endfor %}
    {% endif %}
{% endmacro %}
  1. *args and **kwargs allow you to pass a variable number of arguments to a function. Which means you do not know before hand that how many arguments can be passed to your function. *args is used to send a non-keyworded arguments. **kwargs is for keyworded arguments.
  2. When automatic escaping is enabled, everything is escaped by default except for values explicitly marked as safe. Here the |safe filter is used to mark variables as safe in the template.

Then use render_field in html

<div class="col-sm-10 has-error">
{{ render_field(form.description, class="form-control") }}
</div>

Filter

Jinja provides some build-in filters.

  • capitalize: The first character will be uppercase, all others lowercase.
<h1>{{ basic.location|capitalize }}</h1>
  • join: Return a string which is the concatenation of the strings in the sequence with a separator between elements. Default separator is empty.
<td>{{ value|join('') }}</td>

results matching ""

    No results matching ""