[英]How can I pass a value from a drop down list to another page in flask
I want to have a page where an option is selected from a drop down list that is passed to the next page.我想要一个页面,从传递到下一页的下拉列表中选择一个选项。 The error I receive is "UnboundLocalError: local variable 'currentuser' referenced before assignment".
我收到的错误是“UnboundLocalError:分配前引用的局部变量‘currentuser’”。 I'm not sure how to update the variable globally when an option is selected from the drop down list or how to access the global variable locally in the next page function.
当从下拉列表中选择一个选项或如何在下一页函数中本地访问全局变量时,我不确定如何全局更新变量。 I am new to python and flask, any help would be greatly appreciated!
我是 python 和烧瓶的新手,任何帮助将不胜感激!
app.py应用程序
from flask import Flask, render_template
import sqlite3
app = Flask(__name__)
@app.route('/selectusername')
def selectusername_page():
# connect to database and populate userlist
conn = sqlite3.connect('users.db')
c = conn.cursor()
c.execute("SELECT * FROM users")
userlist = c.fetchall()
conn.close()
return render_template('selectusername.html', userlist=userlist)
@app.route('/showusername')
def showusername_page():
currentuser=currentuser
return render_template('showusername.html', currentuser=currentuser)
if __name__ == '__main__':
app.run(debug=True)
selectusername.html选择用户名.html
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<body>
<button onclick="window.location.href = 'showusername';">Continue</button>
<h1>Select User</h1>
<select id="currentuser">
{% for user in userlist %}
<option value="{{user[0]}}">{{user[0]}}</option>
{% endfor %}
</select>
</body>
</html>
showusername.html显示用户名.html
<h1>Hello {{ currentuser }}</h1>
If you use如果你使用
<form action="/showusername">
and button without JavaScript
and you use name="currentuser"
in <select>
和没有
JavaScript
按钮,你在<select>
使用name="currentuser"
<select name="currentuser">
then it can send selected value in url然后它可以在 url 中发送选定的值
/showusername?currentuser=selected_name
and you can get it in showusername
using request.args
您可以使用
request.args
在showusername
获取它
currentuser = request.args.get("currentuser")
To hide name from url you would have to use POST
method - so you have to set要从 url 中隐藏名称,您必须使用
POST
方法 - 所以您必须设置
<form action="/showusername" method="POST">
and in flask并在烧瓶中
@app.route('/showusername', methods=['POST', 'GET'])
and then you get it using request.form
instead of request.args
然后你使用
request.form
而不是request.args
得到它
currentuser = request.form.get("currentuser")
Full running example完整运行示例
from flask import Flask, render_template, render_template_string, request
app = Flask(__name__)
@app.route('/selectusername')
def selectusername_page():
userlist = [['James'], ['Adam'], ['Mark']]
return render_template_string('''<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<body>
<form action="/showusername">
<button>Continue</button>
<h1>Select User</h1>
<select id="currentuser" name="currentuser">
{% for user in userlist %}
<option value="{{user[0]}}">{{user[0]}}</option>
{% endfor %}
</select>
</form>
</body>
</html>''', userlist=userlist)
@app.route('/showusername', methods=['POST', 'GET'])
def showusername_page():
print('args:', request.args)
print('form:', request.form)
#currentuser = request.args.get("currentuser")
currentuser = request.form.get("currentuser")
return render_template_string('''<h1>Hello {{ currentuser }}</h1>''', currentuser=currentuser)
if __name__ == '__main__':
app.run(debug=True)
If you want to use JavaScript
in button then you would have to use JavaScript
to get selected value and add it to url like如果您想在按钮中使用
JavaScript
,则必须使用JavaScript
来获取选定的值并将其添加到 url 中,例如
window.location.href = 'showusername?currentuser=selected_name'
so it is more complicated and I don't put code in JavaScript
.所以它更复杂,我不把代码放在
JavaScript
。 Maybe someone else will show this.也许其他人会展示这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.