9

I have a blueprint, home, with the prefix / on my Flask app. The blueprint has a static folder and is configured with the static_folder argument. However, linking to the blueprint's static files returns a 404 error, even though the file exists and the url looks right. Why doesn't the blueprint serve the static files?

myproject/
    run.py
    myapp/
        __init__.py
        home/
            __init__.py
            templates/
                index.html
            static/
                css/
                    style.css

myapp/init.py:

from flask import Flask

application = Flask(__name__)

from myproject.home.controllers import home

application.register_blueprint(home, url_prefix='/')

myapp/home/controllers.py:

from flask import Blueprint, render_template

home = Blueprint('home', __name__, template_folder='templates', static_folder='static')

@home.route('/')
def index():
    return render_template('index.html')

myapp/home/templates/index.html:

<head>
<link rel="stylesheet" href="{{url_for('home.static', filename='css/style.css')}}">
</head>
<body>
</body>

myapp/home/static/css/style.css:

body {
    background-color: green;
}
davidism
  • 121,510
  • 29
  • 395
  • 339
niloofar
  • 2,244
  • 5
  • 23
  • 44
  • Maybe you should move your blueprint to init.py file? – metmirr Jan 25 '17 at 14:21
  • No error, just a white page. The body tag does not have any css style, I can't see the backgroun. – niloofar Jan 25 '17 at 14:44
  • and this is more defenation of my project from my last question: http://stackoverflow.com/questions/41828711/flask-blueprint-sqlalchemy-cannot-import-name-db-into-moles-file/41839910#41839910 – niloofar Jan 25 '17 at 14:46

2 Answers2

12

You are getting a conflict with the Flask static folder and your blueprint. Since the blueprint is mounted at /, it shares the same static url as the app, but the app's route takes precedence. Change the static url for the blueprint so it doesn't conflict.

home = Blueprint(
    'home', __name__,
    template_folder='templates',
    static_folder='static',
    static_url_path='/home-static'
)
davidism
  • 121,510
  • 29
  • 395
  • 339
CodeLikeBeaker
  • 20,682
  • 14
  • 79
  • 108
  • I did, now facing with this error `werkzeug.routing.BuildError: Could not build url for endpoint 'static' with values ['filename']. Did you mean 'home.static' instead?` – niloofar Jan 25 '17 at 15:11
  • 1
    I see the problem, change your `url_prefix='/'` to `url_prefix=''` in your `__init__.py` – CodeLikeBeaker Jan 25 '17 at 15:22
  • I did but still the same error. Does it back to `` line? – niloofar Jan 25 '17 at 15:23
  • Before you said you were not getting an error, now it's getting this? I have your complete project setup (based on another of your questions), and this is working 100% for me. – CodeLikeBeaker Jan 25 '17 at 15:29
  • Are you sure you are putting in the right code? This error is if you have filename= in the wrong spot, or it's missing. – CodeLikeBeaker Jan 25 '17 at 15:32
  • Yes I'm sure I'm putting int he right code. none of the solutions worked for me and don't understand why :| – niloofar Jan 26 '17 at 06:53
  • By changing `static_url_path='/home-static'` from home bluprint to `'/static'` and deleteing `static_folder=None` from `application = Flask(__name__, static_folder=None)`, I will get the white page again instead of that error. – niloofar Jan 26 '17 at 07:02
  • I had to set my `url_prefix` to `url_prefix=''` I had it as `/` for home pages but didn't allow any other pages inside that blueprint to be accessed. – A Star Sep 30 '17 at 20:49
5

Finally on the base of friend's answers, I found the correct answer myself. The only changes should be like this:

myapp/init.py:

application = Flask(__name__, static_folder=None)

myapp/home/controllers.py:

home = Blueprint('home', __name__, template_folder='templates', static_folder='static', static_url_path='static')

myapp/home/templates/index.html:

    <link rel="stylesheet" href="{{url_for('home.static', filename='style.css')}}">
niloofar
  • 2,244
  • 5
  • 23
  • 44
  • This was helpful, although for me I set my public blueprint like so: `static_folder='../static', static_url_path='/static'`, with nothing in my main app call. I was then able to successfully able to reference it as `src="{{ url_for('public.static', filename='img/......` – cheevahagadog Sep 21 '18 at 19:46