Home > Blockchain >  Getting bootstrap dropdown value return to work in flask
Getting bootstrap dropdown value return to work in flask

Time:02-15

I am trying to get a value (player) from the dropdown select. Once I click the dropdown button, I want the value 'player' to get sent to flask and render the new player's image on the website. HTML:

<form method="POST">
    <div >
      <select  id="inputGroupSelect04" name="player" value="{{ player }}">
          {% for key, value in team.items() %}
            <option value={{ value }}>{{ value }}</option>
          {% endfor %}
      </select>
      <div >
        <button  type="submit">Select</button>
      </div>
    </div>

</form>
    <img src="/static/{{ player }}.png" alt="{{ player }}">

Flask:

@app.route("/", methods=['POST', 'GET'])
def main_page():
    player = okc[1628983]
    if request.method == "POST":
        print("this posted")
        player = request.form.get('player')
        print(request.form.get('player'))
        return render_template("index.html", team=okc, player=player)
    return render_template("index.html", team=okc, player=player)

CodePudding user response:

instead of your:

player = request.form.get('player')

you can try this:

player = request.form['player']

CodePudding user response:

Button wasn't posting - changed type from button to submit

  • Related