flask - Setting a select element based on data in WTForms -
i need set select element based on data database on page load.
for instance, if database looks this:
+----+--------+ | id | status | +----+--------+ | 0 | foo | | 1 | bar | | 2 | baz | +----+--------+
i load page has 3 rows each have select named status dynamically loaded foo, bar, , baz, respectively.
how set several selects in page dynamically data in database?
i have looked through documentation , couldn't find built in this. have pretty heavily modified field render macro in jinja template can mess with.
edit: code posted below:
the form class:
class itemform(form): status = selectfield( "status", choices=[ ("foo", "foo"), ("bar", "bar"), ("baz", "baz") ] )
the view display actual data database (display.html
):
{% item in items %} <div class="id">{{ item.id) }}</div> <div class="status">{{ form_field(itemform.status, value=item.status) }}</div> {% endfor %}
the macro display form field:
{% macro form_field(field) -%} {% set with_label = kwargs.pop('with_label', false) %} {% set no_label = kwargs.pop('no_label', false) %} {% set placeholder = '' %} {% if not with_label %} {% set placeholder = field.label.text %} {% endif %} <div class="form-group {% if field.errors %}error{% endif %}"> {% if with_label %} <label for="{{ field.id }}" class="control-label"> {{ field.label.text }}{% if field.flags.required %} *{% endif %}: </label> {% endif %} {% set class_ = kwargs.pop('class_', '') %} {% if field.flags.required %} {% set class_ = class_ + ' required' %} {% endif %} {% set prefix = kwargs.pop('prefix', '') %} {% set postfix = kwargs.pop('postfix', '') %} {% if prefix or postfix %} <div class="input-group"> {% endif %} {% if prefix %} <div class="input-group-addon">{{ prefix }}</div> {% endif %} {% if field.type == 'booleanfield' %} <label class="checkbox"> {{ field(class_=class_, **kwargs) }} {% if with_label %} {{ field.label.text|safe }} {% endif %} </label> {% else %} {% set class_ = class_ + ' form-control' %} {% if field.type in ('textfield', 'textareafield', 'passwordfield') %} {% set class_ = class_ + ' input-xlarge' %} {% elif field.type == 'filefield' %} {% set class_ = class_ + ' input-file' %} {% endif %} {% if field.type == "selectfield" %} {{ field(class_=class_, placeholder=placeholder, default=2, **kwargs) }} {% else %} {{ field(class_=class_, placeholder=placeholder, **kwargs) }} {% endif %} {% endif %} {% if postfix %} <div class="input-group-addon">{{ postfix }}</div> {% endif %} {% if prefix or postfix %} </div> {% endif %} {% if field.errors %} <span class="error help-inline">{{ field.errors|join(', ') }}</span> {% endif %} {% if field.description %} <p class="help-block">{{ field.description|safe }}</p> {% endif %} </div> {%- endmacro %}
edit 2: controller code posted below.
controller:
@app.route('/') def index(): itemform = itemform(request.form) itemsquery = item.query.order_by().all() return render_template( "display.html", items=itemsquery, itemform=itemform )
Comments
Post a Comment