提交搜索表单后,无法使用 Flask 应用程序从 url 检索变量 [英] Can't retrieve variable from url with Flask app after submitting search form
问题描述
我想在用户提交搜索表单后呈现一个新视图.我以与其他视图相同的方式制作它,但不幸的是这次没有发生任何事情,我无法从应用程序路由中检索内容.(所以这个问题不是 this 问题,该问题仅在提交表单后出现,它在其他所有情况下都能完美运行.)我在表单中写入一些内容,提交它,然后浏览器中的 url 更改,但无论如何视图都没有更改.我几乎可以肯定这是因为搜索 slug 中的 ?
和 =
,但不知道我应该如何在 Python 代码中处理它们.
实际上,当我提交表单时,我的浏览器会将我重定向到这样的网址:
http://domain/.com/?search=content+from+textfield
这就是我尝试从搜索字段中获取内容并在 Flask 一侧呈现新视图的方式:
@app.route('/?search=', methods=['POST'])def hello_url(url_content):return render_template("search-results.html", searchString = url_content])
如果有人能告诉我正确的方式,我真的很感激,基本上我只想在搜索按钮后检索 <url_content>
在 hello_url
函数中的值敲击.
这是我的 html:
<br><div class="text-center"><button type="submit" class="btn btn-primary btn-lg">搜索!</button></div></表单>
你混淆了使用
捕获的 url 参数和 中访问的查询参数>request.args
.从路由定义中删除查询参数并在视图中访问它.
来自烧瓶导入请求@app.route('/')定义索引():返回 render_template('index.html')@app.route('/搜索')定义搜索():search = request.args.get('search') # 如果没有提交表单,则为 None# 用搜索做一些事情return render_template('search.html', search=search)
index.html
:
I would like to present a new view after the user submits a search form. I made it as the same way as I do with my other views, but unfortunately this time doesn't happens anything, I can't retrieve the content from the app route. (So this question is not a duplicate of this question, the issue occurs only after submitting the form, it works perfectly in every other situation.) I write something into the form, submit it, then the url changes in the browser, but the view doesn't change anyway. I'm almost sure that it's because the ?
and =
in the search slug, but don't know how should I deal with them in the Python code.
Actually when I submit the form my browser redirects me to an url like this:
http://domain/.com/?search=content+from+textfield
And this is how I tried to catch the content from the search field and present a new view on the Flask's side:
@app.route('/?search=<url_content>', methods=['POST'])
def hello_url(url_content):
return render_template("search-results.html", searchString = url_content])
I would really appreciate if somebody could show me the right way, basically I just wanna retrieve the value of <url_content>
inside the hello_url
function after the search button tapped.
Here's my html:
<form>
<div class="form-group">
<input type="search" class="form-control text-center input-lg" id="inputSearch" name="search" placeholder="search">
</div>
<br>
<div class="text-center"><button type="submit" class="btn btn-primary btn-lg">Search!</button></div>
</form>
You're confusing url parameters, which are captured with <variable>
, with query parameters, which are accessed in request.args
. Remove the query parameter from your route definition and access it in the view.
from flask import request
@app.route('/')
def index():
return render_template('index.html')
@app.route('/search')
def search():
search = request.args.get('search') # will be None if form wasn't submitted
# do something with search
return render_template('search.html', search=search)
index.html
:
<form action="{{ url_for('search') }}">
<input name="search"/>
<input type="submit" value="Search"/>
</form>
这篇关于提交搜索表单后,无法使用 Flask 应用程序从 url 检索变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!