Reputation: 5634
While building a small Flask based web app, I came across the need to pass arbitrary data from a Form Field object to the template where it's being rendered. However, I can't seem to find a way to do this.
The only place I think I could add such data was to the kwargs
property of WTForms
Field
objects, but then I seem to have no way to access those properties from the template.
In case you're wondering what I'm trying to accomplish, I'm writing a template macro to ease form rendering, and I need to pass some extra data from the Form
fields objects - mostly layout related, but which will not be HTML attributes for the fields themselves (which is what kwargs
is designed for).
Upvotes: 0
Views: 475
Reputation: 5634
I found an answer here but it's not an answer per se but I comment.
Quoting Crast:
The
description
keyword argument of WTForms fields is allowed to be set at field construction, and is not inspected, just copied directly onto the field, and thus can be any value, not just a string, even a custom attribute. If you want to carry over your own metadata, you can simply use this to carry over any data you may want:TextField(..., description={'placeholder': foo', 'class': bar}
(or even a custom class) then use this attribute in your template for any special metadata you want.
Yes, I know about separating content and presentation and the purpose of the description
property isn't really intended for this kind of use, but it's the only way I found to pass data back to the template where I use a macro to render forms.
To access the passed data inside description
from the template I did something like this:
{% macro render_create_form(form, form_title, enctype=None) %}
<h2>{{ form_title }}</h2>
<form action="" method="post"{% if enctype %} enctype="{{ enctype }}"{% endif %}>
{{ form.hidden_tag() }}
{% for field in form if not field.name == 'csrf_token' %}
{% set class_name = field.description.class %}
{% if field.type == "StringField" or field.type == "PasswordField" or field.type == "BooleanField" or field.type == "SelectField" %}
<div class="{{ class_name }}">{{ field.label }} {{ field }}</div>
{% elif field.type == "NumberField" %}
<div class="{{ class_name }}">{{ field.label }} {{ field(type='number', min=field.description.min, max=field.description.max, placeholder=field.description.placeholder) }}</div>
{% elif field.type == "HiddenField" %}
{{ field }}
{% elif field.type == "SubmitField" %}
<div class="{{ class_name }}">{{ field }}</div>
{% endif %}
{% endfor %}
</form>
{% endmacro %}
Upvotes: 1