【问题标题】:Flask when I wrote app.route('/home') and edited it app.route('/home/') and restore it烧瓶当我写 app.route('/home') 并编辑它 app.route('/home/') 并恢复它
【发布时间】:2021-09-12 21:05:06
【问题描述】:

我正在研究flask,但遇到了问题。

当我写这样的代码时:

@app.route("/reference")
def reference():
    return render_template("reference.html", css="reference", records=records)

http://127.0.0.1:5000/reference 页面正在运行。

然后我在烧瓶文档中找到了'尾斜线'

我想应用它,所以我像这样编辑代码

@app.route("/reference/")
def reference():
    return render_template("reference.html", css="reference", records=records)

它也工作了! http://127.0.0.1:5000/reference/

但是一些问题即将到来。

浏览器无法读取我的 css 文件(在 html 链接中。href=blabla)在 python 终端中更改了日志。

GET /static/css/style.css HTTP/1.1 << before changing
GET /reference/static/css/style.css HTTP/1.1 << after changing

我重定向了css文件路径,

href="static/css/style.css"
to
href="../static/css/style.css"

而且它有效。

我想了解“斜线”的作用。

所以我重置我的代码到第一个代码。

然后 404 not found error 引发,我得到了一个未更改日志

"GET /reference HTTP/1.1" << log for first code
"GET /reference/ HTTP/1.1" << log for second code
"GET /reference/ HTTP/1.1" << log for reset code (== first code)

我有一个问题。 发生了什么?

我不明白为什么它不像以前那样运行。

我读过https://flask.palletsprojects.com/en/2.0.x/quickstart/#unique-urls-redirection-behavior

但我还是不明白发生了什么。

为什么 GET 路径改变了..为什么 GET 路径没有改变..为什么..

我的脑子坏了,我睡不着..请帮帮我..

【问题讨论】:

  • 您是否按照flask.palletsprojects.com/en/2.0.x/tutorial/static 中的描述为您的 CSS URL 使用了模板标签?
  • 是的。在我的 layout.html 中, 就在那里。之所以在文中讲 css,是因为我想讲一个 Symptom。因为 layout.html 有另一个 css 链接。我想将 href="{{ url_for('static', filename='css/style.css') }}" 用于另一个 css,但 css arg 来自服务器,我尝试使用 href="{{ url_for('static ', filename='{{css}}/style.css') }}",它不起作用。所以我必须像文本一样更改我的 CSS URL。
  • 其实我想知道为什么 app.route("/reference) 只在第一次工作,而不是现在。

标签: python flask routes trailing-slash


【解决方案1】:

除了unique-urls-redirection-behavior 文档所说的内容之外,当您使用&lt;img src='some_url'&gt;&lt;link href='some_url'&gt; 之类的标签时,行为可能会有所不同,因为这些网址是由浏览器加载的。。 p>

因此,使用像@app.route("/reference") 这样的路由装饰器在浏览器中加载为example.com/reference,带有href="subpath/file.css" 的链接标签会导致浏览器从example.com/subpath/file.css 加载该资源

另一方面,使用像@app.route("/reference/")(带有尾部斜杠)这样的路由装饰器在浏览器中加载为example.com/reference/(同样带有尾部斜杠)带有href="subpath/file.css" 的链接标签会导致浏览器从example.com/reference/subpath/file.css加载该资源

这可以解释您所看到的行为。


换一种说法,考虑这个测试应用:

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/a')
def a(): return render_template('index.html')

@app.route('/b/') # Note trailing slash
def b(): return render_template('index.html')

templates/index.html与以下模板配对:

<html>
<head>
    <link rel='stylesheet'  href='subpath/file.css' />
</head>

然后进行一些点击并在控制台中观察资产请求(它们会给出 404 错误,因为我还没有真正创建 CSS 文件,这只是为了显示浏览器请求的内容):

加载网址:http://localhost:5000/a

"GET /a HTTP/1.1" 200 -
"GET /subpath/file.css HTTP/1.1" 404 -

加载网址:http://localhost:5000/b/:

"GET /b/ HTTP/1.1" 200 -
"GET /b/subpath/file.css HTTP/1.1" 404 -

当然,包含这些资产的正确方法是使用url_for 函数。

因此,如果我将模板更新为包含:

  <link rel='stylesheet'  href='{{ url_for("static", filename="file.css") }}' />

然后发出同样的请求:

加载网址:http://localhost:5000/a

"GET /a HTTP/1.1" 200 -
"GET /static/file.css HTTP/1.1" 404 -

加载网址:http://localhost:5000/b/

"GET /b/ HTTP/1.1" 200 -
"GET /static/file.css HTTP/1.1" 404 -

正如您在此处所见,静态资源的正确路径将始终呈现,无论端点是否有尾部斜杠。

【讨论】:

  • 感谢您对 url_for 行为的回答。那么你能告诉我为什么 app.route("/a") 在我编辑 app.route("/a/") 并恢复它时不起作用吗?它是一个错误吗?我真正的问题是它。
猜你喜欢
  • 2018-02-17
  • 2019-12-22
  • 2018-10-01
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-03
相关资源
最近更新 更多