我已经尝试阅读Bottle的文档,但是,我仍然不确定静态文件服务是如何工作的。我有一个index.tpl文件,里面有一个css文件,它可以工作。然而,我读到Bottle不会自动提供css文件,如果页面正确加载,这不可能是真的。
然而,我在请求页面时遇到了速度问题。是不是因为我没用return static_file(params go here)?如果有人能弄清楚它们是如何工作的,以及在加载页面时如何使用它们,那就太好了。
服务器代码:
from Bottle import route,run,template,request,static_file
@route('/')
def home():
return template('Templates/index',name=request.environ.get('REMOTE_ADDR'))
run(host='Work-PC',port=9999,debug=True)索引:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type"
content="text/html; charset=ISO-8859-1">
<title>index</title>
<link type="text/css"
href="cssfiles/mainpagecss.css"
rel="stylesheet">
</head>
<body>
<table
style="width: 100%; text-align: left; margin-left: auto; margin-right: auto;"
border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td>
<h1><span class="headertext">
<center>Network
Website</center>
</span></h1>
</td>
</tr>
</tbody>
</table>
%if name!='none':
<p align="right">signed in as: {{name}}</p>
%else:
pass
%end
<br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2"
cellspacing="2">
<tbody>
<tr>
<td>
<table style="text-align: left; width: 100%;" border="0"
cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="width: 15%; vertical-align: top;">
<table style="text-align: left; width: 100%;" border="1"
cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td>Home<br>
<span class="important">Teamspeak Download</span><br>
<span class="important">Teamspeak Information</span></td>
</tr>
</tbody>
</table>
</td>
<td style="vertical-align: top;">
<table style="text-align: left; width: 100%;" border="1"
cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td>
<h1><span style="font-weight: bold;">Network Website</span></h1>
To find all of the needed information relating to the network's social
capabilities, please refer to the links in the side bar.</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</body>
</html>发布于 2012-05-08 02:44:35
正如文档中所指出的,您应该使用静态函数提供静态文件,而css是一个静态文件。静态函数处理安全性和其他一些您可以从源代码中找到的函数。静态函数的path参数应指向存储css文件的目录
发布于 2012-11-07 04:34:29
要使用bottle提供静态文件,您需要使用提供的static_file函数并添加一些额外的路由。以下路由定向静态文件请求,并确保只访问具有正确文件扩展名的文件。
from bottle import get, static_file
# Static Routes
@get("/static/css/<filepath:re:.*\.css>")
def css(filepath):
return static_file(filepath, root="static/css")
@get("/static/font/<filepath:re:.*\.(eot|otf|svg|ttf|woff|woff2?)>")
def font(filepath):
return static_file(filepath, root="static/font")
@get("/static/img/<filepath:re:.*\.(jpg|png|gif|ico|svg)>")
def img(filepath):
return static_file(filepath, root="static/img")
@get("/static/js/<filepath:re:.*\.js>")
def js(filepath):
return static_file(filepath, root="static/js")现在,在html中,您可以像这样引用文件:
<link type="text/css" href="/static/css/main.css" rel="stylesheet">目录布局:
`--static
| `--css
| `--fonts
| `--img
| `--js发布于 2016-06-08 08:37:11
我只是在这里提供一个答案,因为我的一些学生在作业中使用了这个代码,而我对这个解决方案有点担心。
在Bottle中提供静态文件的标准方法是在documentation中
from bottle import static_file
@route('/static/<filepath:path>')
def server_static(filepath):
return static_file(filepath, root='/path/to/your/static/files')这样,静态文件夹下的所有文件都通过以/static开头的URL提供。在HTML中,您需要引用资源的完整URL路径,例如:
<link rel='stylesheet' type='text/css' href='/static/css/style.css'>
Sanketh的答案是,URL空间中任何地方对图像、css文件等的任何引用都是从静态文件夹中的给定文件夹中提供的。因此,/foo/bar/baz/picture.jpg和/picture.jpg都将由static/images/picture.jpg提供。这意味着你不需要担心在你的HTML代码中获得正确的路径,并且你总是可以使用相对文件名(即.只需src="picture.jpg")。
当您尝试部署应用程序时,这种方法的问题就出现了。在生产环境中,您希望静态资源由像nginx这样的web服务器提供服务,而不是由Bottle应用程序提供服务。要实现这一点,它们都应该从URL空间的单个部分提供服务,例如。/static。如果您的代码中到处都是相对文件名,那么它就不能很容易地转换为这个模型。
因此,我建议使用瓶子教程中的三行解决方案,而不是本页列出的更复杂的解决方案。它的代码更简单(因此不太可能出现错误),并且允许您无缝地迁移到生产环境,而无需更改代码。
https://stackoverflow.com/questions/10486224
复制相似问题