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.
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:
url_prefix
when register blueprint.app = Flask(__name__, static_folder=None)
.You need to add URL prefix for your blueprint, like so
app.register_blueprint(dashboard, url_prefix='/dashboard')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With