Python 将下拉菜单中的值传递给 Flask 模板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23557063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:08:48  来源:igfitidea点击:

Passing value from a drop down menu to a Flask template

pythonhtmlsqliteflask

提问by Alex

I am having an issue passing an item selected from an HTML drop down menu to a SQL query.

我在将从 HTML 下拉菜单中选择的项目传递给 SQL 查询时遇到问题。

I'm not exactly sure what concept is missing from my code to make this work. Most examples I have found are in PHP and I'm not sure how to translate it to Python.

我不确定我的代码中缺少什么概念来完成这项工作。我发现的大多数示例都是用 PHP 编写的,我不确定如何将其转换为 Python。

Here is the scenario. I'm using Flask and Sqlite and am try to let the user select an item from an HTML drop down menu. The item selected from the drop down menu will be used in a SQL query to retrieve information from a database and then display those results on a new page.

这是场景。我正在使用 Flask 和 Sqlite,并尝试让用户从 HTML 下拉菜单中选择一个项目。从下拉菜单中选择的项目将用于 SQL 查询以从数据库中检索信息,然后在新页面上显示这些结果。

An example, the user selects "Red" from a drop down that has 3 options (Red, Blue, Green) and clicks a submit button. "Red" will be passed to a SQL query in my app.py file that will retrieve all data from rows where color = "Red". That retrieved data will then be displayed on /results.html.

例如,用户从具有 3 个选项(红色、蓝色、绿色)的下拉列表中选择“红色”并单击提交按钮。“Red”将传递给我的 app.py 文件中的 SQL 查询,该查询将从 color = “Red”的行中检索所有数据。检索到的数据将显示在 /results.html 上。

I believe my problem is that I'm not correctly attributing a value to the items in my drop down menu and then passing that value to my Python code that is running the SQL query. This is my assumption on how this should work, but I might be missing something bigger here.

我相信我的问题是我没有正确地将值分配给下拉菜单中的项目,然后将该值传递给运行 SQL 查询的 Python 代码。这是我对它应该如何工作的假设,但我可能在这里遗漏了一些更大的东西。

I have tried lots of bits of HTML to make this work, but I'm not even sure that is where my problem is occurring. When I hit "Submit" I'm taking the new page, but nothing from the database is displayed

我已经尝试了很多 HTML 来完成这项工作,但我什至不确定这是我的问题发生的地方。当我点击“提交”时,我正在进入新页面,但没有显示数据库中的任何内容

Here is my code, for my 2 views ('/' & 'results.html') and my Python code:

这是我的代码,用于我的 2 个视图 ('/' & 'results.html') 和我的 Python 代码:

@app.route('/results.html', methods=['GET','POST'])
def results():
    g.db = connect_db()

    cur = g.db.execute("SELECT * FROM all_items WHERE name = '{}'".format('Red'))
    posts = [dict(item=row[0], name=row[1]) for row in cur.fetchall()]
    g.db.close()
    return render_template('results.html', posts=posts)

Here is View where the the drop down menus exist

这是下拉菜单所在的视图

<select name="Item_1">
    <option value="Red">Red</option>
    <option value="Green">Green</option>        
</select>

<form name="Item_1" action="results.html" method='POST'>
    <button type="submit">Compare!</button>
</form>

Here is the View where the results from selecting "Red" should be displayed

这是应该显示选择“红色”结果的视图

% extends "template.html" %}
{% block content %}
<h2>Results</h2>

{% for p in posts %}
    <strong>Item:</strong> {{ p.item }}
    <strong>Name:</strong> {{ p.name}}  
{% endfor %}
{% endblock %}

采纳答案by dirn

You need to put your selectinside the form.

你需要把你的select里面form

<form name="Item_1" action="results.html" method='POST'>
    <select name="Item_1">
        <option value="Red">Red</option>
        <option value="Green">Green</option>        
    </select>
    <button type="submit">Compare!</button>
</form>

An even better way to declare the formwould be

一种更好的声明方式form

<form name="Item_1" action="{{ url_for('results') }}" method="POST">

回答by Veerabahu

As mentioned by @dim put the select inside a the form and the chosen value can be got using request.form['Item_1']. However the item being queried is a select, so I would prefer using get instead of POST. From wiki, http://en.wikipedia.org/wiki/POST_(HTTP), POST is used when

正如@dim 所提到的,将选择放在表单中,并且可以使用request.form['Item_1']. 然而被查询的项目是一个选择,所以我更喜欢使用 get 而不是 POST。来自 wiki,http://en.wikipedia.org/wiki/POST_(HTTP),POST 用于

The POST request method is designed to request that a web server accept the data enclosed in the request message's body for storage.[1] It is often used when uploading a file or submitting a completed web form.

So I would rather prefer GET, which can be used to query the database. When using GET, Item_1 can be passed as request parameters and got using flask.request.args

所以我更喜欢GET,它可以用来查询数据库。使用 GET 时,Item_1 可以作为请求参数传递并使用flask.request.args 获取