瓶子的网络应用没有提供静态css文件[英] Bottle web app not serving static css files

本文是小编为大家收集整理的关于瓶子的网络应用没有提供静态css文件的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

尽管我正在使用static_file方法,但我的瓶子Web应用程序仍未为我的main.cs.文件服务.

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看起来像

<!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"的文件夹中,该文件夹在我的根文件夹中

推荐答案

而不是指定您的静态路线

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

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

使其示例表特定于

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

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

其他推荐答案

我可以看到2个问题:

  • CSS文件的路由应以斜线的形式开始,即.

    @route('/<filename>.css')
    
  • 仅将模式的匹配部分传递给stylesheets() filename参数,例如而不是main.css,它将是 main.更改代码:

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

其他推荐答案

或者...重命名您的 main.css 文件 main.tpl ,使用<style>和</style>的书架,将其移至<<强>/视图目录以及其他模板,然后只需添加返回行的开头:

return (template ("main"), template ("list", terms=terms, defins=defins))

本文地址:https://www.itbaoku.cn/post/1975351.html

问题描述

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)

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>

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/')

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

To make it stylesheet specific

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

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'.

其他推荐答案

There are 2 problems that I can see:

  • The route for the CSS files should begin with a slash, ie.

    @route('/<filename>.css')
    
  • Only the matching part of the pattern is passed to stylesheets() in the filename argument, e.g. instead of main.css, it will be main. Change the code to this:

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

其他推荐答案

Alternatively... rename your main.css file to main.tpl, bookend with <style> and </style>, move it into the /views directory along with your other templates, then simply add to the beginning of your return line:

return (template ("main"), template ("list", terms=terms, defins=defins))