Bogdan Doicin
Bogdan Doicin

Reputation: 2416

How can I set the 'static' folder when it is not on the index folder, with Flask?

I want to build a site. I have the index folder and the connected folder. Each folder also has a static folder. In the connected/static folder, I have the connected.css file, which I am trying to access, through my blueprint. However, the final page tries to access the connected.css from the index/static folder.

Where am I mistaking?

Useful code:

__init__.py:

from flask import Flask
from .index import index_routes
from .connected import connected_routes


def create_app():
    app = Flask(__name__, template_folder="templates", static_folder="static")
    app.register_blueprint(index_routes.index_bp)
    app.register_blueprint(connected_routes.connected_bp, url_prefix='/connected')
    return app

connected_routes.py

from flask import Blueprint
from flask import render_template

connected_bp = Blueprint('connected_bp', __name__, template_folder='templates', static_folder='static', url_prefix='/connected')  

@connected_bp.route('/')
def connected():
    return render_template('connected.html', title="Connected to Hattrick")

index_routes.py

from flask import Blueprint
from flask import render_template

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


@index_bp.route('/')
def home():
    return render_template('index.html', title="The Best Match Predictor")

connected.html

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

In the above line I have the problem.

Upvotes: 0

Views: 50

Answers (1)

Maarten
Maarten

Reputation: 492

Maybe give this a try:

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

For some elaboration: https://flask.palletsprojects.com/en/1.1.x/blueprints/#static-files

Upvotes: 1

Related Questions