Flask - 获取值
Flask - Getting values
我想从用户在搜索框中输入的内容中获取 "name" 和 "size" 值。
但明显的问题是我试图从 method: "POST"
hmtl 表单中 获取 值。
我的 html 操作仅引用 {{ url_for('main') }}
html 而不是要在 {{ url_for('search') }} [=35= 中检索这些值的位置].
模板如下:
import sqlite3
from flask import Flask,render_template, url_for, redirect, request
app = Flask(__name__)
conn = sqlite3.connect('shoes.db', check_same_thread=False)
c = conn.cursor()
@app.route("/", methods=["GET", "POST"])
def main():
if request.method == 'GET':
return render_template('main.html')
elif request.method == 'POST':
name = request.form.get('name')
size = request.form.get('size')
return redirect(url_for('search'))
@app.route("/search/", methods=["GET", "POST"])
def search():
# Need to get the 'name' and 'search' values from input, so this way is incorrect.
name = request.args.get("name")
size = request.args.get("size")
c.execute("SELECT * FROM shoes WHERE name LIKE ? AND sizes LIKE ? ORDER BY price",
('%'+name+'%','%'+size+'%'))
p = c.fetchall()
url = [p[i][0] for i in range(len(p))]
names = [p[i][1] for i in range(len(p))]
prices = [p[i][2] for i in range(len(p))]
sizes = [p[i][3] for i in range(len(p))]
shoe_type = [p[i][4] for i in range(len(p))]
return render_template('search.html' , url=url, names=names, prices=prices,
sizes=sizes, shoe_type=shoe_type)
if __name__ == '__main__':
app.run(debug=True)
main.html:
{% extends "layout.html" %}
{% block content %}
<form action = "{{ url_for('main') }}" class="form" method = "POST" >
<h1>Soccer Shoes Finder</h1>
<div class="line-separator"></div>
<div class="container-fluid">
<input name = "name" type="text" placeholder="Name"/>
<input name = "size" type="text" placeholder="Size"/>
<button class="glyphicon glyphicon-search" aria-hidden="true"></button>
</div>
</form>
{% endblock %}
search.html:
{% extends "layout.html" %}
{% block content %}
<div class= "container">
<h2>Results:</h2>
<table class="table table-striped">
<thead>
<tr>
<th>url</th>
<th>Name</th>
<th>Prices</th>
<th>Sizes</th>
<th>Shoe Type</th>
</tr>
</thead>
<tbody>
{% for i in range(url|length) %}
<tr>
<td><a href={{url[i]}}>aa</td>
<td>{{names[i]}}</td>
<td>${{prices[i]}}</td>
<td>{{sizes[i]}}</td>
<td>{{shoe_type[i]}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
有人知道我怎样才能做到这一点吗?希望我说的有道理。谢谢。
简化你的主要路线,似乎没有理由POST这里的数据:
@app.route("/")
def main():
return render_template('main.html')
POST
通过更改 main.html Jinja 中的这一行,将数据直接发送到您的 search 端点模板:
<form action="{{ url_for('search') }}" class="form" method="POST">
由于此页面呈现依赖于表单数据,我只是将其设置为仅接受 POST 方法,但这取决于您。如果您还想支持查询字符串 GET
方法,则需要使用 request.form (or request.values 访问此处的值).
@app.route("/search/", methods=["POST"])
def search():
name = request.form.get("name")
size = request.form.get("size")
假设您的数据库代码正常工作,其余部分应该按预期工作
我想从用户在搜索框中输入的内容中获取 "name" 和 "size" 值。
但明显的问题是我试图从 method: "POST"
hmtl 表单中 获取 值。
我的 html 操作仅引用 {{ url_for('main') }}
html 而不是要在 {{ url_for('search') }} [=35= 中检索这些值的位置].
模板如下:
import sqlite3
from flask import Flask,render_template, url_for, redirect, request
app = Flask(__name__)
conn = sqlite3.connect('shoes.db', check_same_thread=False)
c = conn.cursor()
@app.route("/", methods=["GET", "POST"])
def main():
if request.method == 'GET':
return render_template('main.html')
elif request.method == 'POST':
name = request.form.get('name')
size = request.form.get('size')
return redirect(url_for('search'))
@app.route("/search/", methods=["GET", "POST"])
def search():
# Need to get the 'name' and 'search' values from input, so this way is incorrect.
name = request.args.get("name")
size = request.args.get("size")
c.execute("SELECT * FROM shoes WHERE name LIKE ? AND sizes LIKE ? ORDER BY price",
('%'+name+'%','%'+size+'%'))
p = c.fetchall()
url = [p[i][0] for i in range(len(p))]
names = [p[i][1] for i in range(len(p))]
prices = [p[i][2] for i in range(len(p))]
sizes = [p[i][3] for i in range(len(p))]
shoe_type = [p[i][4] for i in range(len(p))]
return render_template('search.html' , url=url, names=names, prices=prices,
sizes=sizes, shoe_type=shoe_type)
if __name__ == '__main__':
app.run(debug=True)
main.html:
{% extends "layout.html" %}
{% block content %}
<form action = "{{ url_for('main') }}" class="form" method = "POST" >
<h1>Soccer Shoes Finder</h1>
<div class="line-separator"></div>
<div class="container-fluid">
<input name = "name" type="text" placeholder="Name"/>
<input name = "size" type="text" placeholder="Size"/>
<button class="glyphicon glyphicon-search" aria-hidden="true"></button>
</div>
</form>
{% endblock %}
search.html:
{% extends "layout.html" %}
{% block content %}
<div class= "container">
<h2>Results:</h2>
<table class="table table-striped">
<thead>
<tr>
<th>url</th>
<th>Name</th>
<th>Prices</th>
<th>Sizes</th>
<th>Shoe Type</th>
</tr>
</thead>
<tbody>
{% for i in range(url|length) %}
<tr>
<td><a href={{url[i]}}>aa</td>
<td>{{names[i]}}</td>
<td>${{prices[i]}}</td>
<td>{{sizes[i]}}</td>
<td>{{shoe_type[i]}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
有人知道我怎样才能做到这一点吗?希望我说的有道理。谢谢。
简化你的主要路线,似乎没有理由POST这里的数据:
@app.route("/")
def main():
return render_template('main.html')
POST
通过更改 main.html Jinja 中的这一行,将数据直接发送到您的 search 端点模板:
<form action="{{ url_for('search') }}" class="form" method="POST">
由于此页面呈现依赖于表单数据,我只是将其设置为仅接受 POST 方法,但这取决于您。如果您还想支持查询字符串 GET
方法,则需要使用 request.form (or request.values 访问此处的值).
@app.route("/search/", methods=["POST"])
def search():
name = request.form.get("name")
size = request.form.get("size")
假设您的数据库代码正常工作,其余部分应该按预期工作