从类生成的列表不可用返回flask中的render_template

问题描述 投票:0回答:1

我是Python和Flask的新手,我无法弄清楚为什么我没有这里可用的数据列表(ranked_list为空)。

如果我注释掉所有Flask部分并且只是调用getDataFromQuery来获取像普通脚本一样的数据,我可以看到数据并将其打印出来。谁能看到我做错了什么?数据是元组列表。 index.html页面位于代码下方,它位于templates文件夹中。我得到的只是一个带有标题行的空白表。

from flask import Flask
from flask import render_template
from flask import request
from queryProcessing_and_Ranking import *

app = Flask(__name__)

@app.route("/")
@app.route("/<query>")
def index():
    query = request.args.get("query")

    Processing = QueryProcessing()
    ranked_list = Processing.getDataFromQuery( query )
    return render_template( "index.html", ranked_list = ranked_list, user_input = query )

if __name__ == '__main__':
    port = int( os.environ.get('PORT', 5000 ) )
app.run( host='0.0.0.0', port=port, debug=True)
<html>
    <head>
        <title>Product Vertical Search Engine</title>
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
    </head>
    <body>
        <form>
            Product Search: <input type="text" name="query"> <input type="Submit" value="Search">
        </form>
        <h1>Ranked product listing for: {{user_input}}</h1>
        <table border = "1">
            <tr>
                <th>Title</th><th>Price</th>
            </tr>
            {% for item in ranked_list %}
                <tr>
                    <td>{{item[0]}}</td>
                </tr>
            {% endfor %}
        </table>
    </body>
</html>
python flask
1个回答
2
投票

鉴于你的路线设置query可能是None,因此你将None传递给你的.getDataFromQuery方法。

@app.route('/')
def index():

    '''
    Returns "None" if not found.
    So when you open your browser to localhost:5000
    this value is None
    unless you visit localhost:5000/something
    then query = "something"
    '''
    query = request.args.get('query') 
    Processing = QueryProcessing()

    ranked_list = Processing.getDataFromQuery(query) # Value could be None
    return render_template( "index.html", ranked_list = ranked_list, user_input = query )

您还应该删除捕获<query>的路由定义,因为它看起来像是在混淆path parametersquery string parameters的概念

编辑

这看起来像你要做的是搜索表单提交,所以我会做以下事情

@app.route('/')
def index():

    user_input = None
    ranked_list = None
    if request.method == 'POST':
        user_input = request.form['query']
        Processing = QueryProcessing()
        ranked_list = Processing.getDataFromQuery(user_input)

    return render_template("index.html", ranked_list=ranked_list, user_input=user_input) 

HTML文件

<html>
    <head>
        <title>Product Vertical Search Engine</title>
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
    </head>
    <body>
        <form>
            Product Search: <input type="text" name="query"> <input type="Submit" value="Search">
        </form>
        {% if user_input %} <!-- See if you have searched yet -->
        <h1>Ranked product listing for: {{user_input}}</h1>
        <table border = "1">
            <tr>
                <th>Title</th><th>Price</th>
            </tr>
            {% for item in ranked_list %}
                <tr>
                    <td>{{item[0]}}</td>
                </tr>
            {% endfor %}
        </table>
        {% else %} <!-- tell user to search for data or that there is no data -->
        <h1>Search for data</h1>
        {% endif %}
    </body>
</html> 

使用一些Jinja2逻辑明确声明没有任何东西可以获得更好的反馈

© www.soinside.com 2019 - 2024. All rights reserved.