gpt4 book ai didi

python - 使用 Bottlepy 初始化 - CSS 未加载

转载 作者:行者123 更新时间:2023-12-01 05:08:02 24 4
gpt4 key购买 nike

我正在尝试使用从 Initializr 下载的样板文件与 Bottle.py 一起。我显然做错了什么,因为当我只是尝试加载 index.html 时,网站在不使用任何样式表的情况下呈现,并且我在浏览器控制台中收到以下错误:

Use of getUserData() or setUserData() is deprecated.  Use WeakMap or element.dataset instead. requestNotifier.js:52
The stylesheet http://localhost:8080/css/normalize.min.css was not loaded because its MIME type, "text/html", is not "text/css". localhost:8080
The stylesheet http://localhost:8080/css/main.css was not loaded because its MIME type, "text/html", is not "text/css". localhost:8080
SyntaxError: syntax error modernizr-2.6.2-respond-1.1.0.min.js:1
SyntaxError: syntax error plugins.js:1
SyntaxError: syntax error

我的应用程序如下所示:

import bottle  # Web server
from bottle import run, route, static_file, error, template # import request


@route('/')
def index():
return static_file('index.html', root='./html/')

@route('./css/<filename>')
def server__static(filename):
return static_file(filename, root='./css/')

if __name__ == '__main__':
# To run the server, type-in $ python server.py
bottle.debug(True) # display traceback
run(host='localhost', port=8080, reloader=True)

样式表是从 index.html 调用的,如下所示:

    <link type="text/css" rel="stylesheet" href="css/normalize.min.css">
<link type="text/css" rel="stylesheet" href="css/main.css">

我的基本文件夹结构是(剪裁的js文件夹等):

bottle_test.py
- html
index.html
- css
main.css
normalize.min.css

据我了解 bottle doesn't serve static files我确实尝试了到 server-static 的路由,并添加、更改和删除了 mimetype 参数,但无法让它工作。

整个样板文件位于 html 文件夹中,应用程序确实找到了 index.html。我在 Firefox 和 Chrome 中尝试过这个。如果重要的话,我使用的是 Win 8.1 和 anaconda 的 python 2.7。我做错了什么?

最佳答案

你的路径似乎错误。 CSS 路由不应该以 . 开头,似乎它会干扰 Bottle 解析它的方式。并且您的 CSS static_file 根目录无法正确反射(reflect)相对于工作目录的 css 文件夹位置。

这应该可以正常工作:

@route('/css/<filename>')
def server__static(filename):
return static_file(filename, root='./html/css')

关于python - 使用 Bottlepy 初始化 - CSS 未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24721429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com