将值从下拉菜单传递给Flask模板 [英] Passing value from a drop down menu to a Flask template

查看:2756
本文介绍了将值从下拉菜单传递给Flask模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,将从HTML下拉菜单中选择的项目传递给SQL查询。



我不完全确定我的概念是什么代码来使这个工作。我发现的大多数例子都在PHP中,我不知道如何将它翻译成Python。



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



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



我相信我的问题是我没有正确地将值归属到我的下拉菜单中的项目然后将该值传递给正在运行SQL查询的Python代码。这是我对这应该如何工作的假设,但我可能会错过这里更大的东西。



我已经尝试了很多的HTML来做这个工作,米甚至不知道这是我的问题发生的地方。当我点击提交时,我正在浏览新页面,但没有显示任何数据库。

;'results.html')和我的Python代码:

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

cur = g.db.execute(SELECT * FROM格式('Red'))
posts = [dict(item = row [0],name = row [1])for cur.fetchall()]
g.db.close()
return render_template('results.html',posts = posts)


$

 < select name =Item_1> b $ b 

这里是查看下拉菜单的存在位置。
< option value =Red>红色< / option>
< option value =Green>绿色< / option>
< / select>

< form name =Item_1action =results.htmlmethod ='POST'>
< button type =submit>比较!< /按钮>
< / form>

以下是显示选择红色的结果的视图

 %extendstemplate.html%} 
{%block content%}
< h2>结果< / h2>

{%对于帖子中的%}
< strong>项目:< / strong> {{p.item}}
< strong>名称:< / strong> {{p.name}}
{%endfor%}
{%endblock%}


表格中放置选择

 < form name =Item_1action =results.htmlmethod ='POST'> 
< select name =Item_1>
< option value =Red>红色< / option>
< option value =Green>绿色< / option>
< / select>
< button type =submit>比较!< /按钮>
< / form>

更好的方法来声明窗体将是

 < form name =Item_1action ={{url_for('results')}}method = POST > 


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

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.

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.

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.

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.

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

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

@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 %}

解决方案

You need to put your select inside the 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 form would be

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

这篇关于将值从下拉菜单传递给Flask模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆