无法在python加载的模板中引用CSS样式表 [英] Can't reference css stylesheet in template being loaded by python

查看:59
本文介绍了无法在python加载的模板中引用CSS样式表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用Python的新手,在尝试从.tpl文档引用样式表时遇到了一个问题. 我的python,模板和css文档都在同一目录中,但是当我使用CMD将页面加载到"localhost:8080"时,它显示的是模板,但未应用样式.

I'm new to using Python and have came across a problem when trying to reference my style-sheet from a .tpl document. My python, template and css documents are all in the same directory however when I load the page using the CMD to "localhost:8080" it shows the template without the style being applied.

在我的模板文档index.tpl中,我引用了三个样式表:

In my template document index.tpl i have referenceed three stylesheets:

<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" />

我的python文件输出模板:index.py:

And my python file outputs the template: index.py:

from bottle import route,template,debug,run
import sqlite3

@route('/')
def player():
    return template('index.tpl')

debug(True)
run(reloader=True)

推荐答案

我没有使用bottle,但是大多数Web框架都要求您将css/js/images放置在特定目录中(该目录是通过配置设置的).通常将其称为静态"或类似名称.

I haven't used bottle, but most web frameworks require you to put your css/js/images in a particular directory (that you set via configuration). Usually it's called "static" or something similar.

如果您尝试直接在浏览器中加载这些CSS文件,我敢打赌:

I bet if you tried to load those CSS files directly in your browser:

http://localhost:8080/demo.css 

您会得到404.

当前设置的方式是您对传统PHP/CGI东西的处理方式-您的Web服务器正在磁盘上寻找文件并提供服务.框架不会(通常)那样工作-您设置路由规则.

The way you have it currently set up is what you'd do for traditional PHP/CGI stuff - your web server is looking for files on disk and serving them. Frameworks don't (generally) work like that - you set up routing rules.

您使用@route('/')装饰器做到了这一点-通过进行设置,您告诉瓶子对http://localhost:8080/的任何请求都应运行播放器函数并返回其生成的任何内容."请注意,您尚未为css文件设置任何规则.

You did that with the @route('/') decorator - by setting that up, you told bottle "any requests to http:// localhost:8080/ should run the player function and return whatever it generates." Notice you haven't set up any rules for your css files.

另一种可能性是您没有正确引用HTML中的CSS文件.如果您在直接加载CSS文件时没有看到404,请发布HTML,我们可以看看.

The other possibility is you're not referring to the CSS files correctly in your HTML. If you don't get a 404 when you load the CSS files directly, post the HTML and we can take a look at that.

编辑:在瓶子文档中找到了该文件:

Edit: Found this in the bottle doc:

http://bottlepy.org/docs/dev/tutorial.html#routing-static -文件

静态文件(例如图像或CSS文件)不会自动提供.您必须添加路由和回调以控制提供哪些文件以及在哪里找到它们:
Static files such as images or CSS files are not served automatically. You have to add a route and a callback to control which files get served and where to find them:

from bottle import static_file
@route('/static/<filename>')
def server_static(filename):
  return static_file(filename, root='/path/to/your/static/files')

这篇关于无法在python加载的模板中引用CSS样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆