【发布时间】: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