python 瓶子 css 模板

python bottle css template

我正在尝试为我正在处理的项目启用动态样式,以便用户可以根据 his/her 自己的房屋颜色设置样式。

我想做类似的事情

from bottle import route, run, template,

@route('/')
def home():
  return template("sandbox")


@route('/mystyle.css')
def giveCss():
  print("Giving css")
  return template("sandbox_css")

sandbox.tpl:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<table >
  <tr>
    <th>Firstname</th>
    <th>Lastname</th> 
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td> 
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td> 
    <td>94</td>
  </tr>
</table>
</body>
</html>

sandbox_css.tpl

table, th, td {
   border: 1px solid black;
}

然而,css 在这种简单的情况下并未呈现。 将来我希望能够做到这一点:

sandbox_css.tpl

table, th, td {
   border: 1px solid {{tableBorderColor}};
}

sandbox.py

@route('/mystyle.css')
def giveCss():
  print("Giving css")
  return template("sandbox_css", tableBorderColor="black")

所以,我偶然发现了它,因为一些输出 chrome 给出了:

Resource interpreted as Stylesheet but transferred with MIME type text/html

在谷歌搜索更改 MIME 类型时,我发现了这个: How to send xml/application format in bottle?

这让我找到了这样做的解决方案:

@route('/mystyle.css')
def giveCss():
  print("Giving css")
  response.content_type = 'text/css'
  return template("sandbox_css", borderColor="red")

简而言之,我将响应内容类型更改为'text/css'