为什么网站404当我添加a/quot时,为什么要添加a/quot。在URL的末尾(烧瓶)?

发布于 2025-02-02 04:19:43 字数 356 浏览 3 评论 0原文

当我访问(虚构网站)时,它有效:

mywebsite.com/flowers

但是如果我在最后添加一个“/”,我会得到一个“找不到”错误:

mywebsite.com/flowers/

假设我的路线是:

@app.route('/flowers', methods=['GET'])
def xyz():..

我应该重定向“/flowers/to/flowers”,还是我应该添加多个路线?我不确定它应该如何工作,而不会在每个(原始路由 +“/”)中复制整个功能。

When I go to my (imaginary website), it works:

mywebsite.com/flowers

But if I add a "/" at the end, I get a "Not Found" error:

mywebsite.com/flowers/

Assuming my route is:

@app.route('/flowers', methods=['GET'])
def xyz():..

Am I supposed to redirect "/flowers/" to /"flowers" or am I supposed to add multiple routes? I'm not sure how it's supposed to work without copy-pasting my entire function for every (original route + "/").

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖树树初阳… 2025-02-09 04:19:43

在烧瓶中,带有和不落后的URL重定向/的工作方式不同。

@app.route('/works/')
def works():
    return 'This works'

@app.route('/sorry')
def sorry():
    return 'sorry'

works端点的URL具有落后斜线。它类似于文件系统中的文件夹。如果您无需拖延斜线(/works)而访问URL,则使用尾随的斜杠/works/将flask重定向到URL。

对不起端点的URL没有落后的斜线。它类似于文件的路径名。使用尾随的斜杠/sarry/访问URL会产生 404“未找到” 错误。

另外,为了在应用程序级别进行此操作,您可以使用

app.url_map.strict_slashes = False

设置strict_slashes false将与/sorver/sorver/路由一起使用。

以斜线结束的URL路线是分支,其他的是叶子。如果启用了strict_slashes(默认),则访问没有拖延斜线的分支URL将在附加的斜线上重定向到URL。

参考: werkzeug docs

In Flask, the URL redirection with and without trailing / works differently.

@app.route('/works/')
def works():
    return 'This works'

@app.route('/sorry')
def sorry():
    return 'sorry'

The URL for the works endpoint has a trailing slash. It’s similar to a folder in a file system. If you access the URL without a trailing slash (/works), Flask redirects you to the URL with the trailing slash /works/.

The URL for the sorry endpoint does not have a trailing slash. It’s similar to the pathname of a file. Accessing the URL with a trailing slash /sorry/ produces a 404 "Not Found" error.

Alternatively, to make this works at the application level, You can use

app.url_map.strict_slashes = False

Setting strict_slashes to False will work as expected with the /sorry/ route.

URL routes that end with a slash are branches, others are leaves. If strict_slashes is enabled (the default), visiting a branch URL without a trailing slash will redirect to the URL with a slash appended.

Reference: Flask doc , Werkzeug Docs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文