【问题标题】:Routing error with python flask search apppython烧瓶搜索应用程序的路由错误
【发布时间】:2017-02-28 06:18:19
【问题描述】:

我正在尝试为我的烧瓶应用程序提供一个简单的搜索功能。我有以下代码开始搜索

<form action="/search" method=post>
 <input type=text name=search value="{{ request.form.search }}"></br>
 <div class="actions"><input type=submit value="Search"></div>
</form>

这与我的 search/controllers.py 脚本挂钩,看起来像这样

@search.route('/search/')
@search.route('/search/<query>', methods=['GET', 'POST'])
def index(query=None):
    es = current_app.config.get("es")

    q = {"query":{ "multi_match":{"fields":["name","tags","short_desc","description"],"query":query,"fuzziness":"AUTO"}}}
    matches = es.search('products', 'offerings', body=q)
    return render_template('search/results.html', services=matches['_source'])

不幸的是,每当我实际搜索时,都会遇到路由错误:

FormDataRoutingRedirect:向此 URL (http://localhost:8080/search) 发送了一个请求,但路由系统自动向“http://localhost:8080/search/”发出了重定向。 URL 是用斜杠定义的,因此如果在没有斜杠的情况下访问该 URL,Flask 将自动重定向到带有斜杠的 URL。确保将您的 POST 请求直接发送到此 URL,因为我们无法使浏览器或 HTTP 客户端使用表单数据可靠地重定向或无需用户交互。注意:此异常仅在调试模式下引发

我尝试将方法更改为methods=['POST'],但没有任何区别。

【问题讨论】:

    标签: python flask


    【解决方案1】:

    使用url_for('index') 为操作生成正确的网址。

    <form action="{{ url_for('index') }}">
    

    目前,您提交的网址没有尾随 /。 Flask 将此重定向到带有尾随 / 的路由,但 POST 数据在许多浏览器上无法通过重定向,因此 Flask 会警告您该问题。

    【讨论】:

      【解决方案2】:

      正如错误所述,您的表单正在发布到 /search,但您的处理程序是为 /search/ 设置的。使它们相同。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-18
        • 1970-01-01
        • 2016-12-23
        • 2020-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多