Flask raises 404 for static project files when using static route

I have a plan, homewith a prefix /in my Flask application. The plan has a static folder and is configured with an argument static_folder. However, binding to static drawing files returns a 404 error, although the file exists and the URL looks correct. Why the project does not put 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;
}
+4
source share
2 answers

Flask . /, URL-, , . URL- , .

home = Blueprint(
    'home', __name__,
    template_folder='templates',
    static_folder='static',
    static_url_path='/home-static'
)
+4

, , . :

MyApp/init.py:

application = Flask(__name__, static_folder=None)

MyApp//controllers.py:

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

MyApp///index.html

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

Source: https://habr.com/ru/post/1667768/


All Articles