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

本文是小编为大家收集整理的关于不能在被python加载的模板中引用css样式表的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我是新手使用Python的新手,并且在尝试从.TPL文档中引用我的样式表时遇到了一个问题. 我的python,模板和CSS文档都在同一目录中,但是当我使用CMD加载页面" Localhost:8080"时,它显示了模板,而无需应用样式.

在我的模板文档索引中.

<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:

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

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

debug(True)
run(reloader=True)

推荐答案

我没有使用过瓶子,但是大多数Web框架都要求您将CSS/JS/Images放在特定目录中(您通过配置设置).通常称为"静态"或类似的东西.

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

http://localhost:8080/demo.css 

您将获得404.

您当前设置的方式是您对传统的PHP/CGI物品的操作 - 您的Web服务器正在寻找磁盘上的文件并为其提供服务.框架不(通常)这样的工作 - 您设置了路由规则.

您使用@Route('/')装饰器做到了这一点 - 通过设置该瓶子,您告诉瓶子"向http://localhost:8080/应该运行播放器功能并返回其生成的任何请求."请注意,您没有为CSS文件设置任何规则.

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

编辑:在瓶子中找到此内容:

-files

静态文件(例如图像或CSS文件)不会自动提供.您必须添加一条路由和回调才能控制哪些文件得到服务以及在哪里找到它们:
from bottle import static_file
@route('/static/<filename>')
def server_static(filename):
  return static_file(filename, root='/path/to/your/static/files')

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

问题描述

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.

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

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)

推荐答案

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.

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

http://localhost:8080/demo.css 

you'd get a 404.

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.

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.

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-files

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