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

查看:282
本文介绍了在Bottle应用程序中找不到静态文件(404)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经审查了所有与此相关的问题,审查了瓶装教程,审查了瓶装Google小组讨论,并且AFAIK,我所做的一切正确。但是,以某种方式,我无法正确加载CSS文件。我在静态文件上得到404,找不到 http:// localhost:8888 / todo / static / style.css ,根据目录下面的结构,情况并非如此。我使用的是Bottle版本0.11(不稳定);我缺少什么,还是Bottle中的错误?



我的目录结构:

  todo / 
todo.py
static /
style.css

我的todo.py:

  import sqlite3 
从bottle import Bottle,路线,运行,调试,模板,请求,验证,static_file,错误,SimpleTemplate

#仅在从导入的瓶上对mod_wsgi
运行Bottle时才需要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';)
结果= c.fetchall()
c.close()

输出=模板(appPath +'make_table',行=结果,get_url = app.get_url)
返回输出

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

我的html:

 %#template生成元组列表(或列表列表,或元组或...的列表)中的HTML表
< head>
< link href = {{get_url('css',filename ='style.css')}} type = text / css rel = stylesheet />
< / head>
< p>未清项目如下:< / p>
< table border = 1>
对于行中的行:
< tr style = margin:15px;>
%i = 0
%用于行中的col:
%如果i == 0:
< td> {{col}}< / td>
%else:
< td> {{col}}< / td>
%end
%i = i + 1
%end
< td>< a href = / todo / edit / {{row [0]}} >编辑< / a< / td>
< / tr>
%end
< / table>


解决方案

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

现在,如果您使用的是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 ?您的应用程序是否有 / 处理程序?



我还建议您避免使用硬编码路径和concat。将路径片段合并在一起。这样会更干净:

  from os.path import join,目录名
...
appPath = dirname(__ file__)

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


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

这篇关于在Bottle应用程序中找不到静态文件(404)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆