我想在用户提交搜索表单后呈现一个新视图。我的制作方式与处理其他视图的方式相同,但不幸的是这次没有发生任何事情,我无法从应用程序路由中检索内容。 (所以这个问题不是重复的this https://stackoverflow.com/questions/22383458/how-to-obtain-values-of-parameters-of-get-request-in-flask问题,该问题仅在提交表单后发生,它在所有其他情况下都可以正常工作。)我在表单中写入一些内容,提交它,然后浏览器中的网址发生变化,但视图无论如何都不会改变。我几乎可以肯定这是因为?
and =
在搜索 slug 中,但不知道我应该如何在 Python 代码中处理它们。
实际上,当我提交表单时,我的浏览器会将我重定向到如下网址:
http://domain/.com/?search=content+from+textfield
这就是我尝试从搜索字段捕获内容并在 Flask 端呈现新视图的方法:
@app.route('/?search=<url_content>', methods=['POST'])
def hello_url(url_content):
return render_template("search-results.html", searchString = url_content])
如果有人能告诉我正确的方法,我将非常感激,基本上我只是想找回<url_content>
在 - 的里面hello_url
点击搜索按钮后的功能。
这是我的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>
您混淆了 url 参数,这些参数是用<variable>
,带有查询参数,可在request.args
。从路由定义中删除查询参数并在视图中访问它。
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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)