烧瓶-获得价值 [英] Flask - Getting values

查看:68
本文介绍了烧瓶-获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从用户在搜索框中输入的内容中获取名称"和大小"值.

I want to get the "name" and "size" values from what the user typed into a search box.

但是明显的问题是我试图从方法:"POST" hmtl形式获取值.而且我的html操作仅引用 {{url_for('main')}} > html,而不是要在{{url_for('search')}} html中检索这些值的地方.

But the obvious problems are that I'm trying to get values from a method: "POST" hmtl form. And my html action just references the {{ url_for('main') }} html not where want to retrieve these values in the {{ url_for('search') }} html.

以下是模板:

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:

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:

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

谁知道我怎么能做到这一点?希望我所说的是有道理的.谢谢.

Anyone know how could I achieve this? Hopefully what I said makes sense. Thanks.

推荐答案

简化您的主要路线,似乎没有理由在此处发布数据:

Simplify your main route, seems there is no reason to POST the data here:

@app.route("/")
def main():
    return render_template('main.html')

通过更改 main.html Jinja模板中的以下行,将数据直接

POST 直接发送到您的 search 端点:

POST the data instead directly to your search endpoint by changing this line within your main.html Jinja template:

<form action="{{ url_for('search') }}" class="form" method="POST">

由于此页面呈现取决于表单数据,因此我将其设置为仅接受POST方法,但这取决于您.您需要使用 request.form (或 request.values(如果您还希望支持查询字符串 GET 方法).

Since this page rendering is dependent on having form data I'd just set it to only accept POST methods, but this is up to you. You need to access the values here using request.form (or request.values if you also want to support query string GET method).

@app.route("/search/", methods=["POST"])
def search():
    name = request.form.get("name") 
    size = request.form.get("size")

假设您的数据库代码可以正常工作,那么其余的应该可以正常工作

Assuming your database code works the rest of this should work as expected

这篇关于烧瓶-获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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