如果没有,Google Chrome为什么要在我的烧瓶网址后面加上正斜杠? [英] Why is Google Chrome appending a forward slash to my flask url when there is none?

查看:81
本文介绍了如果没有,Google Chrome为什么要在我的烧瓶网址后面加上正斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在各种浏览器上测试基于Flask的网络应用时,我发现Chrome浏览器在我的一条路由和仅一条路由之后附加了 /。此行为导致页面未找到错误,因为我的路线是这样的:

While testing my Flask-based web app on various browsers, I found out that Chrome browser appends a "/" after one of of my routes and only one. This behavior results in a page not found error since my routes is like this:

/dictionary

但是Chrome渲染如下:

But Chrome renders is like this:

/dictionary/

根据烧瓶不同,

这是我的/词典路线和方法:

Here is my /dictionary route and method:

@app.route("/dictionary", methods=["GET", "POST"])
def dictionary():
    results = []
    if request.method == 'POST':
        word = request.form.get("word")
        # do stuff here
        return render_template("results", results=results)
    return render_template("dictionary.html")

我的html网址:

<a href="{{url_for(dictionary)}}"> </a>

它会生成以下预期网址:

It generates the following expected url:

<a href="/dictionary"></a>

请注意,这种奇怪的行为仅在Chrome浏览器中会注意到。它在Opera,Firefox和EI上运行良好。更奇怪的是,它仅在这一条路线上。具有类似方法和网址构建的其他路由正常。
N.B:我可以将路由更改为 / dictionary /,这样它可以工作,但是我希望保留 / url_example这样的路由。先感谢您。

Please notice that this strange behavior is only noticed on Chrome browser. It works fine on Opera, Firefox, and EI. And more strangely it's only on this one route. Other routes with similar methods and url building act normal. N.B: I could change my route to "/dictionary/" so it works, but I wish to preserve "/url_example" like routes. Thank you in advance.

推荐答案

/字符为正斜杠。表示 strict_slashes ,它要求URL设置后缀为斜杠,否则要求URL不包含后缀斜杠,默认情况下启用,问题。

The "/" character is a forward slash. Said this, strict_slashes, which requires that the URL has a trailing slash if set, or requires that the URL does not have a trailing slash otherwise, is enabled by default, causing this issue.

您可以通过以下操作在整个应用中禁用严格的斜杠:

You can disable strict slashes in your entire app by doing:

app = Flask(__name__)
app.url_map.strict_slashes = False

您可以从以下答案中了解更多信息: https://stackoverflow.com/a/33285603

You can learn more about this from this answer: https://stackoverflow.com/a/33285603

这篇关于如果没有,Google Chrome为什么要在我的烧瓶网址后面加上正斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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