我是使用Python的新手,在尝试从.tpl文档引用我的样式表时遇到了一个问题.我的python,template和css文件都在同一个目录中,但是当我使用CMD将页面加载到"localhost:8080"时,它显示的模板没有应用样式.
在我的模板文档index.tpl中,我引用了三个样式表:
<link rel="stylesheet" type="text/css" href="demo.css" />
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="stylesheet" type="text/css" href="animate-custom.css" />
Run Code Online (Sandbox Code Playgroud)
我的python文件输出模板:index.py:
from bottle import route,template,debug,run
import sqlite3
@route('/')
def player():
return template('index.tpl')
debug(True)
run(reloader=True)
Run Code Online (Sandbox Code Playgroud)
我没有使用过瓶子,但是大多数Web框架都要求你将css/js/images放在一个特定的目录中(通过配置设置).通常它被称为"静态"或类似的东西.
我敢打赌,如果你试图直接在浏览器中加载这些CSS文件:
http://localhost:8080/demo.css
Run Code Online (Sandbox Code Playgroud)
你会得到404.
您当前设置的方式就是您为传统的PHP/CGI工作做的事情 - 您的Web服务器正在寻找磁盘上的文件并为它们提供服务.框架(通常)不会那样工作 - 您设置了路由规则.
你用@route('/')装饰器做到了 - 通过设置它,你告诉瓶子"对http:// localhost:8080 /的任何请求应该运行播放器功能并返回它生成的任何内容." 请注意,您尚未为css文件设置任何规则.
另一种可能性是你没有在HTML中正确引用CSS文件.如果您在直接加载CSS文件时没有获得404,请发布HTML,我们可以看一下.
编辑:在瓶子文件中找到这个:
http://bottlepy.org/docs/dev/tutorial.html#routing-static-files
静态文件(如图像或CSS文件)不会自动提供.您必须添加路由和回调来控制要提供的文件以及在哪里找到它们:
from bottle import static_file
@route('/static/<filename>')
def server_static(filename):
return static_file(filename, root='/path/to/your/static/files')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5441 次 |
| 最近记录: |