为什么网站404当我添加a/quot时,为什么要添加a/quot。在URL的末尾(烧瓶)?
当我访问(虚构网站)时,它有效:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在烧瓶中,带有和不落后的URL重定向
/
的工作方式不同。works
端点的URL具有落后斜线。它类似于文件系统中的文件夹。如果您无需拖延斜线(/works
)而访问URL,则使用尾随的斜杠/works/
将flask重定向到URL。对不起
端点的URL没有落后的斜线。它类似于文件的路径名。使用尾随的斜杠/sarry/
访问URL会产生 404“未找到” 错误。另外,为了在应用程序级别进行此操作,您可以使用
设置
strict_slashes
false将与/sorver/sorver/
路由一起使用。以斜线结束的URL路线是分支,其他的是叶子。如果启用了
strict_slashes
(默认),则访问没有拖延斜线的分支URL将在附加的斜线上重定向到URL。参考: werkzeug docs
In Flask, the URL redirection with and without trailing
/
works differently.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
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