【问题标题】:initializr with bottlepy - css not loaded带有bottlepy的initializr-未加载css
【发布时间】:2014-07-13 09:49:39
【问题描述】:

我正在尝试使用从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 上。我做错了什么?

【问题讨论】:

    标签: python url-routing bottle static-files initializr


    【解决方案1】:

    您的路径似乎错误。 CSS 路由不应该以. 开头,似乎它会干扰 Bottle 解析它的方式。而且您的 CSS static_file root 没有正确反映 css 文件夹相对于工作目录的位置。

    这应该没问题:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-22
      • 2017-05-30
      • 2023-02-02
      • 2013-05-14
      • 2021-09-29
      • 2020-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多