如何使用 flask 和 sqlalchemy 在仪表板中显示搜索结果

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

我正在尝试为节目和场地制作一个搜索栏。我可以在带有选项 venue 或 show 的框中搜索,但我无法打印结果并且我的日志中没有错误。

@app.route('/search', methods = ['GET','POST'])
def search():
    
    search_type = request.args.get('type')
    query = request.args.get('q')

    # Search for shows and venues that match the query
    if search_type == 'venues':
        search_results = Venue.query.filter(Venue.venue_name.ilike(f"%{query}%")).all()
    elif search_type == 'shows':
        search_results = Show.query.filter(Show.show_name.ilike(f"%{query}%")).all()
    else:
        search_results = []

这是我的 python 代码,但它正在打印一个空列表。

{% if results %}
        <h2>Search Results for "{{ query }}"</h2>
        {% if search_type == 'venues' %}
            <ul>
                {% for venue in results %}
                    <li><a href="/view_venues/{{ venue.venue_id }}">{{ venue.venue_name }}</a></li>
                {% endfor %}
            </ul>
        {% elif search_type == 'shows' %}
            <ul>
                {% for show in results %}
                    <li><a href="/view_shows/{{ show.show_id }}">{{ show.show_name }}</a></li>
                {% endfor %}
            </ul>
        {% endif %}
{% endif %}

这是我的 html 代码,其中应显示场地或节目列表。你能帮我解决这个错误吗?我得到的只是两者的空列表。

python-3.x flask-sqlalchemy
© www.soinside.com 2019 - 2024. All rights reserved.