Flask 应用程序在路线中搜索结果返回上一个

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

我无法忍受很长时间。
当我尝试地址中的第二个搜索(mySecondSearch)时,它返回上一个搜索(mySearch) - http://127.0.0.1:5000/search?key=mySearch

(但查询有效 - 我得到带有键 mySecondSearch 的列表的模板)

如何使用与我的请求相关的密钥获取地址

header.html

<form method="POST" action="{{ url_for('views.search', key=slugy) }}" role="search" class="hiddenSearch">

{{ search_form.hidden_tag() }}
<input class="form-control" type="search" 
                            placeholder="" 
                            aria-label="Search"   
                            name="searched" 
                            minlength="3" 
                            maxlength="44" 
                            oninvalid="this.setCustomValidity('')" 
                            oninput="this.setCustomValidity('')" 
                            title=""/>
</form>

视图.py

@views.route('/search/', methods=["POST"])
def search():
    page_title = ""
    clean = re.compile('<.*?>')
    search_form = SearchForm()
    posts = Poems.query

    if request.method == "POST" and search_form.validate_on_submit():
        # Get Data from submitted form
        searched = search_form.searched.data
        poem_list.searched = searched
        slugy = slugify(searched)

        # Query 
        page = request.args.get('page', 1, type=int)
        posts = posts.filter(Poems.con_keyword.ilike('%' + searched + '%'))
        posts = posts.order_by(Poems.title).paginate(per_page=7, page=page, error_out=True)

        return render_template("content/search.html", 
                       search_form=search_form, 
                       searched=searched, 
                       page_title=page_title,
                       posts=posts, 
                       clean=clean, re=re, slugy=slugy)

    else:
    flash("!")
    return render_template("content/search.html", 
                           search_form=search_form, 
                           page_title=page_title, 
                           posts=posts, clean=clean, re=re)

搜索.html

<section id="search">

    <div class="content">
        {% if searched %}
         <h1> you ask -
            <span class="src-ed">{{ searched[0:12] }}...</span><br/>we found:
        </h1>

...etc

我尝试删除缓存,或不保存任何数据,-什么都没有
经过测试 - 一切都完美地返回
AI - 没有什么可以帮助我(我尝试了它的代码 - 和我的一样工作)

python flask search
1个回答
0
投票

这很疯狂,但代码是从免费课程中获取的,所以基本上写错了。我读了很多(文档)所以解决方案是!!!重新写一下!

所以现在可以了! http://127.0.0.1:5000/search?key=myNewSearch

代码


@views.route('/search', methods=['GET', 'POST'])
def search():
     page_title = "Поиск"
     clean = re.compile('<.*?>')
     search_form = SearchForm()
     query = request.args.get('key')

     if query:
         posts = Poems.query.filter(Poems.con_keyword.ilike(f'%{query}%')).all()
     else:
         posts = Poems.query.all()

     return render_template("content/search.html", search_form=search_form, 
                            page_title=page_title, posts=posts, 
                            clean=clean, re=re, key=query)


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