【问题标题】:How to display a list into Flask select?如何在 Flask 选择中显示列表?
【发布时间】:2022-01-21 10:01:41
【问题描述】:

我创建了一个包含两个列表的函数,并希望将其显示在 seq.html 文件中。

@app.route('/datasets/<dataset>/sequentialprocess', methods=['POST', 'GET'])
def sequential_process(dataset=dataset):
    models = request.form.get('models')
    strategy = request.form.get('strategy')

    strategies = ['MEI (exploit)', 'MU (explore)', 'MLI (exploit & explore)', 'MEID (exploit)', 'MLID (explore & exploit)']
     mdl = ['Gaussian Process Regression (GPR)', 'lolo Random Forrest (RF)', 'Decision Trees (DT)', 'Random Forrest (RFscikit)']

     return render_template('seq.html', strategies=strategies, mdl=mdl)

这是我在seq.html 中使用jinja 的列表之一:

<div class="col-sm-3">
  <h7>Strategy:</h7>
    <select name="strategy" class="form-control is-valid" required>
        {% for st in strategies %}
          <option value="{{st}}">{{st}}</option>
        {% endfor %}
    </select>
</div>

strategies 列表不会显示在seq.html 中。谁能告诉我这里出了什么问题?

【问题讨论】:

    标签: python flask


    【解决方案1】:

    您的 HTML 存在问题。您缺少一些 HTML 标记。

    试试这个:

    <!DOCTYPE html>
    <html>
      <body>
    <div class="col-sm-3">
        <h7>Strategy:</h7>
          <select name="strategy" class="form-control is-valid" required>
              {% for st in strategies %}
                <option value="{{st}}">{{st}}</option>
              {% endfor %}
          </select>
      </div>
    </body>
    </html>
    

    【讨论】:

    • HTML 标签在我这里没有包含。
    • 我运行了代码,它在我的端正常工作。如果你有标签,那么问题不在于这段代码。
    猜你喜欢
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 2013-12-30
    • 2018-02-24
    • 2013-02-25
    • 1970-01-01
    相关资源
    最近更新 更多