简体   繁体   English

用 bottle.py 加载 CSS 和 JavaScript

[英]Loading CSS and JavaScript with with bottle.py

I am trying to write an app with Bottle.py我正在尝试使用Bottle.py编写一个应用程序

<link href="bootstrap.min.css" rel="stylesheet" media="screen">
<script src="http://d3js.org/d3.v2.js"></script>  
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="bootstrap.min.js"></script>

My view copies these import statements without actually importing the css + js files themselves.我的视图复制了这些导入语句,而没有实际导入 css + js 文件本身。

I have included a static template, as thetutorial suggests正如教程所建议的,我已经包含了一个static模板

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

How do I modify the HTML so my browser reads the *.js, *.css files?如何修改 HTML 以便我的浏览器读取*.js, *.css文件?

I used (as an example)我用过(作为例子)

... href="/arch/favicon.ico"

and I have我有

@route('/arch/<filename>')
def fileget(filename):

for static files.对于 static 个文件。

Using :path filter allow match such URLs like /bootstrap.min.js or /js/bootstrap.min.js .使用:path过滤器允许匹配这样的 URL,如/bootstrap.min.js/js/bootstrap.min.js In the first case Bottle returns file /usr/home/project/client/bootstrap.min.js , in second /usr/home/project/client/js/bootstrap.min.js .在第一种情况下,Bottle 返回文件/usr/home/project/client/bootstrap.min.js ,在第二种情况下返回/usr/home/project/client/js/bootstrap.min.js

@route('<path:path>')
def server_static(path):
    return static_file(path, root='/usr/home/project/client')

:path matches all characters including the slash character in a non-greedy way and can be used to match more than one path segment. :path 以非贪婪方式匹配包括斜线字符在内的所有字符,可用于匹配多个路径段。

Use regex matching to serve .css & .js files:使用正则表达式匹配服务.css.js文件:

@route('/<file:re:.*\.(css|js)>')
def serve_cssjs_suffixed_file(file):
    return static_file(file)

Some of notable options to the 'static_file' function are: “static_file”function 的一些值得注意的选项是:

root='target_dir' point to directory containing the files root='target_dir'指向包含文件的目录

mimetype='text/css' manually pass MIME type if bottle guessed incorrectly mimetype='text/css'如果瓶子猜错了,手动传递 MIME 类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM