Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask Blueprint can't find static folder

Tags:

python

flask

I've set up a Blueprint in my Flask application, but I can't seem to get my static folder to work. I keep getting 404 errors when it tries to reach them:

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 -

The css one also appends static twice. The JS has the correct /static but doesn't seem to work. Right now, my static folder is in the blueprint root path (app/dashboard). I tried putting it into app/static but I get the same exact errors.

I have the following set up:

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)

In app/templates/layout.html, I have a line that references two static files as follows:

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

My app/dashboard/static directory:

$ 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

Any idea what is going on here? How can I structure my Blueprint properly? I've followed the instructions on the Flask documentation and I get this error.

Thank you.

like image 993
darksky Avatar asked Sep 11 '14 19:09

darksky


2 Answers

Look like you have issue with two same routes for application and blueprint. See my another answer part 3.

Your application route /static/<path:path> to .static endpoint.

Your blueprint route /static/<path:path> to dashboard.static endpoint, because you do not have url_prefix when register blueprint and have /static - static folder prefix.

So you can use one of next solutions:

  1. use only application static folder.
  2. set url_prefix when register blueprint.
  3. use another static folder prefix for blueprint.
  4. disable application static folder app = Flask(__name__, static_folder=None).
  5. use hacks with static endpoint descriptor.
like image 130
tbicr Avatar answered Oct 14 '22 19:10

tbicr


You need to add URL prefix for your blueprint, like so

app.register_blueprint(dashboard, url_prefix='/dashboard')
like image 37
Thanh DK Avatar answered Oct 14 '22 17:10

Thanh DK