How to grab the value from the drop-down menu using flask

Viewed 2485

I am trying to grab the value from the drop-down menu after clicking the submit button for which the UI looks like this :

The code for the above is as follows :

app.py

from flask import Flask, render_template, request
app = Flask(__name__)
app.debug = True


@app.route('/', methods=['GET'])
def dropdown():
    colours = ['SBI', 'Kotak', 'Citi', 'AMEX', 'BOB', 'AXIS', 'HDFC', 'IDBI', 'YES', 'IndusInd']
    return render_template('feba.html', colours=colours)

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

index.html is in the templates folder

<!DOCTYPE html>
<html lang="en">

<style>
    body {text-align:center}
</style>

<head>
    <meta charset="UTF-8">
</head>

<body>

    <img src="{{url_for('static', filename='logo_1.jpg')}}" align="middle" />
    <h1>KARDFINDER</h1>
    <h2>json file generator for banks</h2>

        <select name="colour" method="GET" action="/">
      <option value="{{colours[0]}}" selected>{{colours[0]}}</option>
      {% for colour in colours[1:] %}
        <option value="{{colour}}">{{colour}}</option>
      {% endfor %}
        </select>

      <input type= "submit" name="submitbutton" value ="Submit"></body>
</html>


logo_1.jpg is in the static folder

My main aim to post this question is to get a solution with the help of which I can grab the value from the drop-down menu after the submit button is clicked and I want that value stored in a variable in the app.py file

Please Help me with this.

2 Answers

below code works on me,

from flask import Flask, render_template, request, redirect
app = Flask(__name__)
app.debug = True


@app.route('/', methods=['GET'])
def dropdown():
    colours = ['SBI', 'Kotak', 'Citi', 'AMEX', 'BOB', 'AXIS', 'HDFC', 'IDBI', 'YES', 'IndusInd']
    return render_template('feba.html', colours=colours)


@app.route('/dropdown', methods = ['POST'])
def dropp():
    dropdownval = request.form.get('colour')
    print(dropdownval)
    return redirect("/", code=302)
if __name__ == "__main__":
    app.run()

<!DOCTYPE html>
<html lang="en">

<style>
    body {text-align:center}
</style>

<head>
    <meta charset="UTF-8">
</head>

<body>
    <img src="{{url_for('static', filename='logo_1.jpg')}}" align="middle" />
    <h1>KARDFINDER</h1>
    <h2>json file generator for banks</h2>
     <form method="POST" action="/dropdown">
        <select name="colour">
      <option value="{{colours[0]}}" selected>{{colours[0]}}</option>
      {% for colour in colours[1:] %}
        <option value="{{colour}}">{{colour}}</option>
      {% endfor %}
        </select>
      <input type="submit" value ="Submit">
      </form>
</body>
</html>

you should learn http requests, html forms and ajax

You could also just combine the index and drop down pages into one:

from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
app.debug = True

@app.route('/', methods=['GET','POST'])
def dropdown():
    colours = ['SBI', 'Kotak', 'Citi', 'AMEX', 'BOB', 'AXIS', 'HDFC', 'IDBI', 'YES', 'IndusInd']
    if request.method=='POST':
        var = request.form['var']
    return render_template('feba.html', colours=colours)

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

This is a bit simpler. You can use the same html code as Mert Celik but change action="/dropdown" to action="/"

Related