简体   繁体   English

如何将下拉列表中的值传递到烧瓶中的另一个页面

[英]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.argsshowusername获取它

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.

相关问题 Django:如何在下拉列表中进行选择并重定向到另一个页面? - Django: How can I take the choice in drop-down list and redirect to another page? 如何在烧瓶中通过URL路由获取下拉值并更改页面? - How to get a drop down value and change the page by url routing in flask? 如何从烧瓶中的下拉框中打印值? - How to print the value from the drop down box in flask? 如何使用 flask 从下拉菜单中获取值 - How to grab the value from the drop-down menu using flask 从下拉菜单中逐页阅读-找不到第二页上的下拉菜单 - Read a page after another from a drop down menu - Can't find the drop down on 2nd page 如何通过 flask 应用程序访问 html 页面,然后从同一 Z319C34606A7D4A9C76 访问另一个 html 页面 - How can I access html page through flask app and then another html page from the same flask app 如何使用Python和Selenium点击页面上的多个下拉列表? - How can I click on multiple drop down list on a page with Python and Selenium? 如何将列表的每个值传递给另一个子进程? - How can I pass each value of a list to another subprocess? 下拉列表依赖于另一个下拉tkinter - Drop down list dependent from another drop down tkinter 如何使用beautifulsoup在下拉列表中获取所选值? - How can I get the selected value in a drop-down list using beautifulsoup?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM