无法找到Bottle应用程序中的静态文件(404)。[英] Static files in a Bottle application cannot be found (404)

本文是小编为大家收集整理的关于无法找到Bottle应用程序中的静态文件(404)。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在这里审查了有关此问题的所有问题,审查了瓶子教程,审查了Google Group讨论的瓶子,而Afaik,我正在做所有事情.但是,不知何故,我无法将我的CSS文件正确加载.我在静态文件上获得了404,找不到http://localhost:8888/todo/static/style.css,根据下面的目录结构,情况并非如此.我正在使用0.11版(不稳定)的瓶子;我缺少什么,还是瓶中的错误?

我的目录结构:

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>

推荐答案

我不太了解您的部署. /Applications/MAMP/htdocs/路径以及代码中缺少app.run的路径,建议您在Apache下运行此操作.是生产部署吗?对于开发任务,您应该使用Botter的内置开发服务器.添加一个app.run()在todo.py的末端,然后完成.

现在,如果您使用的是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'))
    ...

其他推荐答案

事实证明,它与瓶子无关,与加载应用程序的WSGI文件有关.我没有将操作系统更改为正确的路径;它指向WSGI脚本所在的文件夹.显然,那里没有CSS文件.一旦我在SGI脚本中纠正了我的目录,一切都可以.换句话说:

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

需要是

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

因为我的WSGI脚本与应用程序本身不同(MOD_WSGI建议使用此方法).感谢所有人的帮助!

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

问题描述

I've reviewed all the questions here about this, reviewed the bottle tutorial, reviewed the bottle google group discussions, and AFAIK, I'm doing everything right. Somehow, though, I can't get my CSS file to load properly. I'm getting a 404 on the static file, that http://localhost:8888/todo/static/style.css is not found, which, according to the directory structure below, should not be the case. I'm using version 0.11 (unstable) of Bottle; is there anything I'm missing, or is this a bug in Bottle?

My directory structure:

todo/
   todo.py
   static/
      style.css

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

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

推荐答案

I don't quite understand your deployment. The /Applications/MAMP/htdocs/ path, along with the lack of app.run in your code, suggest that you're running this under Apache. Is it a production deployment? For dev tasks you are supposed to use Bottle's built-in dev server, you know. Add a single app.run() towards the end of your todo.py, and you're done.

Now if you are using Apache, the most probably root cause is this line: static_file(filename, root='./static'). With mod_wsgi, you are not guaranteed that the working directory is equal to the directory where your todo.py is placed. In fact, it will almost never be.

You are using absolute paths for the database and the template, do so for the static files:

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

Next, I don't understand where your app is mounted. The URL http://localhost:8888/todo/static/style.css suggests that the mount point is /todo, but the route for the todo_list handler is again /todo. Is the full path supposed to be http://localhost/todo/todo? Does your app have a / handler?

I'd also suggest to avoid hard-coding paths and concat'ing the path fragments together. This would be cleaner:

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

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

其他推荐答案

It turns out that it had nothing to do with Bottle and everything to do with my wsgi file that loaded the application. I did not change my os.path to the correct path; it was pointing to the folder where the wsgi script was located. Obviously, there was no css file there. Once I corrected my directory in the sgi script, everything worked. In other words:

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

needed to be

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

because my wsgi script was in a different directory than the application itself (mod_wsgi recommends this approach). Thanks for all the help everyone!