Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropdown menu in Flask/Jinja from list

I'm trying to write a simple dropdown menu from a list in flask. For some reason, the dropdown list is empty... I'd appreciate all hints :-)

app.py (fragment)

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

run_ligand.html (fragment)

<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>
like image 981
AirelleJab Avatar asked Nov 25 '25 05:11

AirelleJab


1 Answers

The dropdown list not empty. Check that you are on the right endpoint (http://localhost:5000/getLigand) that you have set in the route method.

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_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>
like image 134
mmuecke Avatar answered Nov 27 '25 19:11

mmuecke