You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask下拉筛选功能无数据返回问题求助

Flask下拉筛选仅显示表头无数据的问题修复

核心问题分析

你的代码存在三个关键逻辑错误,导致无数据返回:

  1. 表单未指定提交目标,默认提交到/search3路由,而负责展示结果的/results路由未处理查询逻辑,也未传递数据到模板
  2. 结果模板results.html使用了不存在的变量ProfileSearch,且原路由未将查询结果传递给该页面
  3. 表格结构不规范,tbody被嵌套在循环内,且thead缺少<tr>标签

修复后的代码实现

1. Flask路由修改

调整路由职责,让/search3仅返回筛选页面,/results处理查询并返回结果:

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

@app.route('/results')
def results():
    category = request.args.get('category', '')
    query = Profile.query
    if category:
        query = query.filter_by(category=category)
    profiles = query.all()
    # 显式传递查询结果给模板
    return render_template('results.html', profiles=profiles)

2. 筛选页面(search3.html)修改

指定表单提交目标为/results,规范默认选项的空值:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="{{url_for('static', filename = 'style/style1.css')}}" rel="stylesheet">
    <title>Search</title>
</head>
<body>
    <!-- 指定表单提交到结果路由 -->
    <form action="/results" method="GET">
        <label>Select a Category</label>
        <select name="category">
            <option value="">Select a category</option>
            {% for value in ('Kitchen', 'Pay', 'Medical', 'Food',) -%}
            <option value="{{ value }}">{{ value }}</option>
            {% endfor -%}
        </select>
        <button type="submit">Search</button>
    </form>
</body>
</html>

3. 结果页面(results.html)修改

修正循环变量名,规范表格结构,添加无数据提示:

<!DOCTYPE html>
<html>
<head>
  <title>Search Results</title>
  <link href="{{url_for('static', filename = 'style/style1.css')}}" rel="stylesheet">
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Merchant</th>
                <th>Other</th>
                <th>Price</th>
                <th>DrCr</th>
                <th>Category</th>
                <th>Payment Method</th>
                <th>Date</th>
                <th>#</th>
            </tr>
        </thead>
        <tbody>
            {% for data in profiles %}
            <tr>
                <td>{{data.id}}</td>
                <td>{{data.merchant}}</td>
                <td>{{data.cMerchantOther}}</td>
                <td>{{data.price}}</td>
                <td>{{data.drcr}}</td>
                <td>{{data.category}}</td>
                <td>{{data.pmtmethod}}</td>
                <td>{{data.updated}}</td>
                <td><a href="/delete/{{data.id}}" type="button">Delete</a></td>
            </tr>
            <!-- 无匹配数据时的提示 -->
            {% else %}
            <tr>
                <td colspan="9">没有找到匹配的数据</td>
            </tr>
            {% endfor%}
        </tbody>
    </table>
</body>
</html>

额外调试建议

  • 检查数据库中category字段的值是否与下拉选项的字符串完全匹配(注意大小写、空格)
  • 在results路由中添加print(len(profiles)),确认是否查询到数据
  • 避免使用**locals()传递变量,显式传递需要的变量更清晰且安全

内容的提问来源于stack exchange,提问作者Rocky N

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 05:23:27