【问题标题】:Static files in a Bottle application cannot be found (404)找不到 Bottle 应用程序中的静态文件 (404)
【发布时间】:2012-06-06 03:02:32
【问题描述】:

我已经在这里查看了所有关于此的问题,查看了瓶子教程,查看了瓶子谷歌小组讨论,并且 AFAIK,我做的一切都是正确的。但是,不知何故,我无法正确加载我的 CSS 文件。我在静态文件上得到 404,找不到 http://localhost:8888/todo/static/style.css,根据下面的目录结构,不应该是这种情况。我正在使用 0.11 版(不稳定)的瓶子;有什么我遗漏的吗,或者这是 Bottle 中的错误?

我的目录结构:

todo/
   todo.py
   static/
      style.css

我的 todo.py:

import sqlite3
from bottle import Bottle, route, run, debug, template, request, validate, static_file, error, SimpleTemplate

# only needed when you run Bottle on mod_wsgi
from bottle import default_app
app = Bottle()
default_app.push(app)

appPath = '/Applications/MAMP/htdocs/todo/'


@app.route('/todo')
def todo_list():

    conn = sqlite3.connect(appPath + 'todo.db')
    c = conn.cursor()
    c.execute("SELECT id, task FROM todo WHERE status LIKE '1';")
    result = c.fetchall()
    c.close()

    output = template(appPath + 'make_table', rows=result, get_url=app.get_url)
    return output

@route('/static/:filename#.*#', name='css')
def server_static(filename):
    return static_file(filename, root='./static')

我的html:

%#template to generate a HTML table from a list of tuples (or list of lists, or tuple of tuples or ...)
<head>
<link href="{{ get_url('css', filename='style.css') }}" type="text/css" rel="stylesheet" />
</head>
<p>The open items are as follows:</p>
<table border="1">
%for row in rows:
  <tr style="margin:15px;">
  %i = 0
  %for col in row:
    %if i == 0:
        <td>{{col}}</td>
    %else:
        <td>{{col}}</td>
    %end
    %i = i + 1
  %end
  <td><a href="/todo/edit/{{row[0]}}">Edit</a></td>
  </tr>
%end
</table>

【问题讨论】:

  • 我也有很多问题。你能在控制台中显示 GET 吗?
  • 只需“GET localhost:8888/todo/static/style.css 404(未找到)”
  • 你有 todo/todo.py static/style.css。尝试使用 todo/static/style.css。看来您的 dir 结构与 pgm 不匹配。
  • @f p 试试在哪里?我在 html 中尝试了 todo/static/style.css,得到了 todo/static/todo/static/style.css 的 404。
  • 磁盘上的文件在哪里?

标签: python bottle


【解决方案1】:

我不太了解您的部署。 /Applications/MAMP/htdocs/ 路径以及代码中缺少 app.run 表明您在 Apache 下运行它。是生产部署吗?对于开发任务,你应该使用 Bottle 的内置开发服务器,你知道的。在todo.py 的末尾添加一个app.run(),就完成了。

现在,如果您使用的是 Apache,最可能的根本原因是这一行:static_file(filename, root='./static')。使用 mod_wsgi,您不能保证工作目录与放置 todo.py 的目录相同。事实上,它几乎永远不会。

您正在为数据库和模板使用绝对路径,请为静态文件这样做:

@route('/static/:filename#.*#', name='css')
def server_static(filename):
    return static_file(filename, root=os.path.join(appPath, 'static'))

接下来,我不明白您的应用安装在哪里。 URL http://localhost:8888/todo/static/style.css 表明挂载点是/todo,但todo_list 处理程序的路由又是/todo。完整路径应该是http://localhost/todo/todo 吗?您的应用是否有 / 处理程序?

我还建议避免硬编码路径并将路径片段连接在一起。这样会更干净:

from os.path import join, dirname
...
appPath = dirname(__file__)

@app.route('/todo')
def todo_list():
    conn = sqlite3.connect(join(appPath, 'todo.db'))
    ...

【讨论】:

  • 我的部署正在使用 mod_wsgi 针对 MAMP apache 堆栈进行开发。我能够让工作在 Bottle 的开发服务器上运行;现在我正在尝试开发我将如何推出生产,这就是导致问题的原因。我会尝试加入而不是串联。 localhost:8888 是 apache 根目录。有一个 todo 文件夹,这是存储 todo.py 代码的地方。路径是 localhost:8888/todo/todo,虽然我没有添加“/”处理程序,但在我测试时这很令人困惑但可以工作。我实际上并没有计划一个待办事项应用程序:-)
  • @Shawn:啊,我明白了。 Bottle 开发服务器的当前目录通常是正确的,所以这个问题很少暴露。 join 很好,但真正重要的是传递给static_file 的绝对路径(想想appPath)。
  • 如何在运行时确定绝对路径是什么?我的网络检查器显示正在 /todo/static/style.css 中搜索 css 文件,如果绝对路径遵循 /MAMP/htdocs/,这将是正确的...如果不是,当然它不会找到它(尽管我仍然对为什么 404 路径看起来正确感到困惑)。
  • @Shawn:澄清一​​下:据我了解您的评论,/todo/static/style.css 是 URL 部分,/Applications/MAMP/... 是文件系统路径。您一定混淆了这两件事。
【解决方案2】:

事实证明,它与 Bottle 无关,而与我加载应用程序的 wsgi 文件有关。我没有将我的 os.path 更改为正确的路径;它指向 wsgi 脚本所在的文件夹。显然,那里没有css文件。一旦我在 sgi 脚本中更正了我的目录,一切正常。换句话说:

os.chdir(os.path.dirname(__file__))

需要

os.chdir('Applications/MAMP/htdocs/todo')

因为我的 wsgi 脚本与应用程序本身位于不同的目录中(mod_wsgi 推荐这种方法)。感谢大家的帮助!

【讨论】:

  • os.chdir 是推荐的,但还不够。 CWD 可以随时更改,您不应该依赖它。按照我的建议,在server_static 中提供静态文件的完整路径。
  • 会的。感谢您的所有帮助。
猜你喜欢
  • 2015-06-04
  • 1970-01-01
  • 1970-01-01
  • 2019-01-12
  • 2018-06-16
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
相关资源
最近更新 更多