[英]How to grab the value from the drop-down menu using flask
I am trying to grab the value from the drop-down menu after clicking the submit button for which the UI looks like this:在单击 UI 如下所示的提交按钮后,我试图从下拉菜单中获取值:
The code for the above is as follows:上述代码如下:
app.py应用程序.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 index.html在模板文件夹中
<!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 logo_1.jpg 位于 static 文件夹中
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我发布此问题的主要目的是获得一个解决方案,借助该解决方案,我可以在单击提交按钮后从下拉菜单中获取值,并且我希望将该值存储在 app.py 文件中的变量中
Please Help me with this.请帮我解决一下这个。
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
你应该学习
http requests
, html forms
和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="/"
您可以使用与 Mert Celik 相同的 html 代码,但将 action="/dropdown" 更改为 action="/"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.