App不提供静态CSS文件

App不提供静态CSS文件

本文介绍了Bottle Web App不提供静态CSS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管我使用的是static_file方法,但是我的Bottle Web应用程序仍未提供main.css文件.

My bottle web application is not serving my main.css file despite the fact I am using the static_file method.

app.py

from bottle import *
from xml.dom import minidom
@route('/')
def index():
    return template("index")

@route('/glossaryXML')
def glossary():
    doc_def = minidom.parse("table_definitions.xml")
    terms = doc_def.getElementsByTagName("str_term")
    defins = doc_def.getElementsByTagName("str_definition")
    return template("list", terms=terms, defins=defins)

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

@error(404)
def fourofour(error):
    return "Error"

run(host='localhost', port=8080, debug=True)

我要访问的页面是索引页面,其中index.tpl看起来像

The page I am trying to access is the index page, in which index.tpl looks like

<!DOCTYPE HTML>
<html>
    <head>
        <title>ICT Applications Glossary</title>
        <link type="text/css" href="main.css" rel="stylesheet">
    </head>
    <body>
        It works
    </body>
</html>

我的CSS文件位于我的根文件夹中的名为"static"的文件夹中

My CSS file is located in a folder named "static" which is in my root folder

推荐答案

请改为指定您的静态路由

Instead specify your static route like this

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

这将为您的静态目录中的任何文件提供服务,而不仅仅是CSS.

This will serve any file in your static directory though not just css.

使其成为样式表专用

@get('/<filename:re:.*\.css>')
def stylesheets(filename):
    return static_file(filename, root='static/')

注意:对于后一种选项,您可以将样式表放在其自己的目录"static/css"或"css"目录中,并将其与其他静态资源(脚本,图像等)分开只需将root参数指定为该目录即可,例如'root ='static/css'.

Note: for the latter option you could put stylesheets in their own directory 'static/css' or just 'css' and keep them separate from other static resources (scripts, images etc.) to do this just specify the root parameter to be that directory e.g. `root='static/css'.

这篇关于Bottle Web App不提供静态CSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 12:26