Flask路径中的尾部斜杠 [英] Trailing slash in Flask route
问题描述
例如,采取以下两条路线.
app = Flask(__name__)
@app.route("/somewhere")
def no_trailing_slash():
#case one
@app.route("/someplace/")
def with_trailing_slash():
#case two
根据文档,可以理解以下内容:>
-
在一种情况下,对路由
"/somewhere/"
的请求将返回404响应."/somewhere"
有效. -
在第二种情况下,
"/someplace/"
有效,并且"/someplace"
将重定向到"/someplace/"
我想看到的行为是情况二行为的'inverse'.例如"/someplace/"
将重定向到"/someplace"
,而不是其他方式.有没有办法定义采取此行为的途径?
据我所知,可以在路由上设置strict_slashes=False
,以有效地获得情况二的情况二,但我想做的是获取重定向行为,以始终将其重定向到URL 没有后跟斜杠.
我想到的一种解决方案是使用404的错误处理程序,类似这样. (不确定这是否行得通)
@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
但是我想知道是否有更好的解决方案,例如某种类型的嵌入式应用程序配置,类似于我可以在全球范围内应用的strict_slashes=False
.也许是蓝图或URL规则?
您正在使用strict_slashes
进行正确的跟踪,可以在Flask应用程序本身上对其进行配置.这会将每个创建的路由的strict_slashes
标志设置为False
app = Flask('my_app')
app.url_map.strict_slashes = False
然后,您可以使用before_request
检测尾随的/
进行重定向.使用before_request
将使您不需要将特殊逻辑分别应用于每个路由
@app.before_request
def clear_trailing():
from flask import redirect, request
rp = request.path
if rp != '/' and rp.endswith('/'):
return redirect(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屋!