【问题标题】:Connect CSS with Bottle将 CSS 与 Bottle 连接起来
【发布时间】:2019-04-30 01:12:37
【问题描述】:

我有这段代码可以将 CSS 文件与 HTML 连接起来:

from bottle import static_file

@route("/static/<filename>")
def static(filename):
    return static_file(filename, root="static")

然后我有这个代码:

@route("/edit/<name>")
def edit(name):
    return template("edit")

问题是我的模板 edit.html 无法连接到静态文件夹中的 CSS 文件。但是当我有这样的代码时它可以工作:

@route("/edit/")
def edit(name):
    return template("edit")

...没有名称标签

【问题讨论】:

  • 我很困惑。 edit 的第二个版本根本不应该工作;它应该会失败,因为 Bottle 会尝试使用零参数调用您的 edit 函数,而它需要一个参数 (TypeError)。

标签: python-3.x bottle


【解决方案1】:

我假设文件“edit.html”中的 CSS 引用如下所示:

<link rel="stylesheet" href="static/styles.css">

我个人通过在CSS文件路径的开头添加斜杠来解决这个问题:

<link rel="stylesheet" href="/static/styles.css">

这样,连接指向站点根目录中的“静态”文件夹。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 2017-09-24
    • 2014-08-16
    • 2023-03-29
    • 2020-01-20
    相关资源
    最近更新 更多