Flask路径中的尾部斜杠 [英] Trailing slash in Flask route

查看:337
本文介绍了Flask路径中的尾部斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,采取以下两条路线.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆