Home > database >  How can I locate my static folder in flask with a divisional blueprint?
How can I locate my static folder in flask with a divisional blueprint?

Time:11-06

I've been trying to do a divisional blueprint but I'm having a hard time locating my individual static folders per blueprint. can someone help me see what's missing?

Here's my code in route.py

from flask import Blueprint, render_template

admin = Blueprint('admin', __name__,
                template_folder="templates",
                static_folder="static")

@admin.route('/admin')
def admin_home():
    return render_template("admin_home.html")

here's my directory

>website
.   .>admin
.   .  .>static
.   .  .    admin_style.css
.   .  .    index.js
.   .  .>templates
.   .  .__init__.py
.   .  .routes.py
.   .>public
.   .>api
.   .>users
.   .__init__.py
.main.py

CodePudding user response:

Try adding the 'static_url_path' argument to the blueprint config:

from flask import Blueprint, render_template

admin = Blueprint('admin', __name__,
                template_folder="templates",
                static_folder="static",
                static_url_path="/admin/static")

@admin.route('/admin')
def admin_home():
    return render_template("admin_home.html")

Assuming your static directory contains a folder named css with your css files in it, you would then access these static files in your HTML like so:

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

The full-stop before the static is important here - without it, the template will try to use the static folder in the application's parent directory. The css/styles.css is simply just the folder followed by the filename you're trying to link to. So this could be img/my_img.png or js/my_js.js

Bit of documentation here if it helps: Flask - Static Files

  • Related