列表中Flask/Jinja中的下拉菜单

2024-05-14 18:45:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图从flask中的列表中编写一个简单的下拉菜单。 由于某种原因,下拉列表为空。。。我将感谢所有提示:-)

app.py(片段)

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

运行\u 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>

Tags: nameformapp列表forhtmlpostselect
1条回答
网友
1楼 · 发布于 2024-05-14 18:45:44

下拉列表不为空。检查您是否位于route方法中设置的右端点(http://localhost:5000/getLigand

app.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_.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>

相关问题 更多 >

    热门问题