python - 瓶静态文件

标签 python python-2.7 bottle

我已经尝试阅读 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>

最佳答案

要使用 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

关于python - 瓶静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10486224/

相关文章:

python - "for chunk in response.iter_content(1024)"引发 StreamConsumedError() 异常

python - 如何在Python中从Json中获取起源-命运矩阵?

python - pandas 使用 if/truth 语句将函数应用于数据框的列

python - 获取 Python 中 if 语句中满足哪个或条件

python - 如何在 python 中按下一个键时停止程序?

python - 使用请求正文的单元测试 bottle py 应用程序导致 KeyError : 'wsgi.input'

python - 如何将输出转换为json格式?

python - 编辑一个 discord.py 嵌入,就好像有人在上面有 "commented"

python - 在Python中是否有任何理由使用collections.namedtuple而不是typing.NamedTuple?

ssl - Cloud9 IDE : Can't connect to my server on a custom port