使用Postgres的烧瓶搜索栏

2024-05-16 23:36:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个烧瓶项目,我想启用一个搜索栏。通常我会创建一个路由,但是搜索栏将url带到?query=并绕过我创建的路由。我试图做一条类似于搜索栏创建的路线,但没有成功。我正试图使用psycopg2使用postgresql实现此功能。

@newsbeta.route("/newsbeta/<query>")
def get_query(query):
    cur.execute(f"SELECT * FROM test WHERE to_tsvector('english',title) @@ to_tsquery('english','{query}');")
    searchquery = []
    for i in range(10):
        searchquery.append(cur.fetchone())
        return render_template('search.html', title='News', searchquery=searchquery)

预期的代码是,当我在搜索栏中编写查询时,它将从postgres数据库中获取查询,并返回包含信息的渲染模板。当我在搜索栏中查询某个东西时,如果我手动转到/newsba/query,这将有效。

用户@roy说了一些可以回答其他问题的方法,但如果有人能启发我,我不知道如何改变这种说法您必须更改模式,以便它重定向到另一个视图,即def show_Uresults()。这也是你无法共享url的原因,即。示例.com/search?查询@roy='some text'


Tags: to项目url路由search烧瓶englishtitle
1条回答
网友
1楼 · 发布于 2024-05-16 23:36:28
@newsbeta.route("/search")
def get_query():
    query = request.args.get("search")
    cur.execute(f"SELECT * FROM test WHERE to_tsvector('english',title) @@ to_tsquery('english','{query}');")
    searchquery = []
    for i in range(10):
        searchquery.append(cur.fetchone())
    return render_template('search.html', title='News', searchquery=searchquery)

#set html 
form action="/search" methods=['GET'] name= "search" id="query"

相关问题 更多 >