Flask 路线中的尾部斜线 [英] Trailing slash in Flask route
问题描述
以下面两条路线为例.
app = Flask(__name__)@app.route("/某处")def no_trailing_slash():#案例一@app.route("/某地/")def with_trailing_slash():#案例二
根据文档,理解如下:>
在第一种情况下,对路由
"/somewhere/"
的请求将返回 404 响应."/somewhere"
有效.在第二种情况下,
"/someplace/"
是有效的,"/someplace"
将重定向到"/someplace/"
>
我希望看到的行为是情况二行为的'逆'.例如"/someplace/"
将重定向到 "/someplace"
而不是相反.有没有办法定义一条路线来采取这种行为?
根据我的理解,可以在路由上设置 strict_slashes=False
以在情况一中有效地获得与情况二相同的行为,但我想做的是将重定向行为设置为始终重定向到没有尾部斜杠的 URL.
我想到的一种解决方案是使用 404 的错误处理程序,类似这样.(不确定这是否有效)
@app.errorhandler(404)def not_found(e):如果 request.path.endswith("/") 和 request.path[:-1] 在 all_endpoints 中:返回重定向(request.path[:-1]),302返回render_template(404.html"),404
但我想知道是否有更好的解决方案,例如某种插入式应用程序配置,类似于我可以全局应用的 strict_slashes=False
.也许是蓝图或网址规则?
您可以使用 strict_slashes
进行正确的跟踪,您可以在 Flask 应用程序本身上进行配置.这将为每个创建的路由设置 strict_slashes
标志为 False
app = Flask('my_app')app.url_map.strict_slashes = False
然后您可以使用 before_request
来检测尾随的 /
以进行重定向.使用 before_request
将允许您不需要将特殊逻辑单独应用于每个路由
@app.before_requestdef clear_trailing():从烧瓶导入重定向,请求rp = request.path如果 rp != '/' 和 rp.endswith('/'):返回重定向(rp [:-1])
Take for example the following two routes.
app = Flask(__name__)
@app.route("/somewhere")
def no_trailing_slash():
#case one
@app.route("/someplace/")
def with_trailing_slash():
#case two
According to the docs the following is understood:
In case one, a request for the route
"/somewhere/"
will return a 404 response."/somewhere"
is valid.In case two,
"/someplace/"
is valid and"/someplace"
will redirect to"/someplace/"
The behavior I would like to see is the 'inverse' of the case two behavior. e.g. "/someplace/"
will redirect to "/someplace"
rather than the other way around. Is there a way to define a route to take on this behavior?
From my understanding, strict_slashes=False
can be set on the route to get effectively the same behavior of case two in case one, but what I'd like to do is get the redirect behavior to always redirect to the URL without the trailing slash.
One solution I've thought of using would be using an error handler for 404's, something like this. (Not sure if this would even work)
@app.errorhandler(404)
def not_found(e):
if request.path.endswith("/") and request.path[:-1] in all_endpoints:
return redirect(request.path[:-1]), 302
return render_template("404.html"), 404
But I'm wondering if there's a better solution, like a drop-in app configuration of some sort, similar to strict_slashes=False
that I can apply globally. Maybe a blueprint or url rule?
You are on the right tracking with using strict_slashes
, which you can configure on the Flask app itself. This will set the strict_slashes
flag to False
for every route that is created
app = Flask('my_app')
app.url_map.strict_slashes = False
Then you can use before_request
to detect the trailing /
for a redirect. Using before_request
will allow you to not require special logic to be applied to each route individually
@app.before_request
def clear_trailing():
from flask import redirect, request
rp = request.path
if rp != '/' and rp.endswith('/'):
return redirect(rp[:-1])
这篇关于Flask 路线中的尾部斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!