从词典列表中填充WTForms选择字段

时间:2020-06-08 10:54:13

标签: python flask flask-wtforms

因此,当前我正在尝试使用从下面显示的字典列表中获取的特定值填充选择字段。

engineerResult = [{'engineerName': 'Test', 'engineerPassword': 'password', 'engineerUsername': 'TestEngineer'},{'engineerName': 'Test2', 'engineerPassword': 'password2', 'engineerUsername': 'TestEngineer2'}]

我的目标是仅使用字段“ engineerUsername”(即“ TestEngineer”和“ TestEngineer2”)填充选择字段

以下是我到目前为止所做的事情。

app.py

class MaintenanceForm(Form):
    engineerName = SelectField('Engineer')

@app.route('/report_vehicle/<string:vehicleID>', methods=['GET', 'POST'])
@is_logged_in
def add_maintenance(vehicleID):
    obj = requestsUtil()
    form = MaintenanceForm(request.form)
    engineerResult = obj.get_engineers()

    for i in engineerResult:
        form.engineerName.choices = i['engineerUsername']

    return render_template('report_vehicle.html', form=form)

HTML

{% extends 'layout.html' %}

{% block body %}
  <h1>Report Vehicle</h1>
  {% from "includes/_formhelpers.html" import render_field %}
  <form method="POST" action="">
    <div class="form-group">
      {{ render_field(form.engineerName, class_="form-control") }}
    </div>
    <p><input class="btn btn-primary" type="submit" value="Submit">
  </form>
{% endblock %}

我得到的当前输出:Screenshot current output

0 个答案:

没有答案