Flask Blueprint无法找到静态文件夹

时间:2014-09-11 19:56:04

标签: python flask

我在Flask应用程序中设置了一个蓝图,但我似乎无法使我的静态文件夹工作。我试图联系到他时遇到404错误:

127.0.0.1 - - [11/Sep/2014 15:14:20] "GET /static/js/bootstrap.min.js HTTP/1.1" 404 -
127.0.0.1 - - [11/Sep/2014 15:14:20] "GET /static/static/css/bootstrap.min.css HTTP/1.1" 404 -

css one也会附加静态两次。 JS具有正确/静态但似乎不起作用。现在,我的静态文件夹位于蓝图根路径(app/dashboard)中。我尝试将其放入app/static,但我得到了同样的错误。

我有以下设置:

app/dashboard/__init__.py

from flask import Blueprint
dashboard = Blueprint('dashboard', __name__, template_folder='templates', static_folder='static')
from application.dashboard import controllers

app/__init__.py

# Blueprints      
from flask import Blueprint
from application.dashboard import dashboard
app.register_blueprint(dashboard)

app/templates/layout.html中,我有一行引用两个静态文件,如下所示:

<link rel="stylesheet" type="text/css" href="{{ url_for('dashboard.static', filename='css/bootstrap.min.css') }}">
<script src="{{ url_for('dashboard.static', filename='js/bootstrap.min.js') }}"></script>

我的app/dashboard/static目录:

$ tree application/dashboard/static/
application/dashboard/static/
├── css
│   ├── bootstrap-theme.css
│   ├── bootstrap-theme.css.map
│   ├── bootstrap-theme.min.css
│   ├── bootstrap.css
│   ├── bootstrap.css.map
│   └── bootstrap.min.css
├── fonts
│   ├── glyphicons-halflings-regular.eot
│   ├── glyphicons-halflings-regular.svg
│   ├── glyphicons-halflings-regular.ttf
│   └── glyphicons-halflings-regular.woff
└── js
    ├── bootstrap.js
    └── bootstrap.min.js

知道这里发生了什么吗?如何正确构建我的蓝图?我已按照Flask文档中的说明操作,我收到此错误。

谢谢。

3 个答案:

答案 0 :(得分:10)

看起来您的应用程序和蓝图有两个相同的路径问题。查看我的another answer part 3

您的申请路线/static/<path:path>.static终点。

您的蓝图路由/static/<path:path>dashboard.static端点,因为您在注册蓝图时没有url_prefix并且拥有/static - 静态文件夹前缀。

所以你可以使用下一个解决方案之一:

  1. 仅使用应用程序静态文件夹。
  2. 在注册蓝图时设置url_prefix
  3. 使用蓝图的另一个静态文件夹前缀。
  4. 禁用应用程序静态文件夹app = Flask(__name__, static_folder=None)
  5. 使用hacks with static endpoint descriptor

答案 1 :(得分:0)

您需要为您的蓝图添加网址前缀,例如

app.register_blueprint(dashboard, url_prefix='/dashboard')

答案 2 :(得分:-1)

为什么不使用nginx?它在性能方面更快,您不必编写代码来提供静态内容。