首页 > 解决方案 > 列表中 Flask/Jinja 中的下拉菜单

问题描述

我正在尝试从烧瓶中的列表中编写一个简单的下拉菜单。出于某种原因,下拉列表是空的......我很感激所有提示:-)

app.py(片段)

@app.route("/getLigand", methods=["GET","POST"])
def dropdown():
    colours = ["Red", "Blue", "Black", "Orange"]
    return render_template("run_ligand.html", colours=colours)

run_ligand.html(片段)

<form name="Item_1" action="/getLigand" method="POST">
    <label for="exampleFormControlFile2">Choose colour</label>
        <select name=colours>
            {% for colour in colours %}
                <option value="{{colour}}" SELECTED>{{colour}}</option>
            {% endfor %}     
        </select>
</form>

标签: pythonflaskjinja2

解决方案


下拉列表不为空。检查您是否位于您在路由方法中设置的正确端点 ( http://localhost:5000/getLigand )。

应用程序.py

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/getLigand", methods=["GET","POST"])
def dropdown():
    colours = ["Red", "Blue", "Black", "Orange"]
    return render_template("run_ligand.html", colours=colours)

if __name__ == "__main__":
    app.run()

run_ligand.html

<!doctype html>
<html lang="en">
  <body>
    <form name="Item_1" action="/getLigand" method="POST">
      <label for="exampleFormControlFile2">Choose colour</label>
        <select name="colours">
          {% for colour in colours %}
            <option value="{{ colour }}" SELECTED>{{ colour }}</option>
          {% endfor %}     
        </select>
    </form>
  </body>
</html>

推荐阅读