Search code examples
flaskblueprint

Flask one Blueprint use wrong Static Folder


I'm currently writing a web application in Flask and using Blueprints. However, a blueprint uses the wrong static folder, although the configuration should be the same. Where is my mistake?

Main.py

 self.app = Flask(__name__)
    self.app.register_blueprint(bp_start)
    self.app.register_blueprint(bp_player, url_prefix="/player")
    self.app.register_blueprint(bp_management, url_prefix="/admin")
    self.app.register_blueprint(bp_club, url_prefix="/club")

bp_club.py

from flask import Blueprint, render_template, request, redirect, url_for, session, current_app, flash
from webapp.database import Database


bp_club = Blueprint("bp_club", __name__, static_folder="static")

bp_management.py

from flask import Blueprint, render_template, request, redirect, url_for, session, current_app, flash
from webapp.database import Database
import hashlib

bp_management = Blueprint("bp_management", __name__, static_folder="static")

bp_player.py

from flask import Blueprint, render_template, request, redirect, url_for, session, current_app, flash
from webapp.database import Database
import hashlib

bp_player = Blueprint("bp_player", __name__, static_folder="static")

Only the bp_player Blueprint dont found the static Folder:

127.0.0.1 - - [30/Nov/2020 00:25:02] "GET /player/edit/static/css/responsive.css HTTP/1.1" 404 -

127.0.0.1 - - [30/Nov/2020 00:34:16] "GET /club/static/script/progressbar.js HTTP/1.1" 200

I only had one Static Folder in Root dir.

Edit: In root dir is the python file to start the programm. all blueprints in webapp

├── Data
├── DB
├── logs
├── venv
│   ├── bin
│   ├── include
│   ├── lib
│   │   
│   └── lib64 -> lib
└── webapp
    ├── __pycache__
    ├── static
    │   ├── css
    │   ├── extra-images
    │   ├── fonts
    │   ├── images
    │   │   ├── avatar
    │   │   └── fancybox
    │   └── script
    └── templates

bp_player.py

from flask import Blueprint, render_template, request, redirect, url_for, 
session, current_app, flash
from webapp.database import Database
import hashlib


bp_player = Blueprint("bp_player", __name__, static_folder="static")



@bp_player.route('/logout/<id>')
def logout(id):
    if session["my_ID"] == id:
        session.pop["my_ID"]
        session.pop["owner"]
    return redirect("/admin/login")

@bp_player.route('/edit/<id>')
def edit(id):
    if session["my_ID"] == id or session["owner"] == 1:
        db = Database(current_app.config["DATABASE"])
        if request.method == "GET":
            where = {"id": id}
            user = db.queryBuilder("select", "player", dict_where=where)
            return render_template("player_edit.html", session=session, 
                                  user=user, nav="player")
    else:
        flash("Please LogIn")
        return redirect("/player/view/" + id, code=303)

Template.html

<link href="static/css/bootstrap.css" rel="stylesheet">
    <link href="static/css/font-awesome.css" rel="stylesheet">
    <link href="static/css/flaticon.css" rel="stylesheet">
    <link href="static/css/slick-slider.css" rel="stylesheet">
    <link href="static/css/fancybox.css" rel="stylesheet">
    <link href="static/style.css" rel="stylesheet">
    <link href="static/css/color.css" rel="stylesheet">
    <link href="static/css/responsive.css" rel="stylesheet">
    <link href="static/css/dark-scheme.css" rel="stylesheet">
    <script type="text/javascript" src="static/script/jquery.js"></script>

Works with changes in template.html

<link href=" {{ url_for('static', filename='css/bootstrap.css') }} " rel="stylesheet">
<link href=" {{ url_for('static', filename='css/awesome.css') }}" rel="stylesheet">
<link href=" {{ url_for('static', filename='css/flaticon.css')  }}" rel="stylesheet">
<link href=" {{ url_for('static', filename='css/slick-slider.css')  }}" rel="stylesheet">
<link href=" {{ url_for('static', filename='css/fancybox.css')  }}" rel="stylesheet">
<link href=" {{ url_for('static', filename='style.css')  }}" rel="stylesheet">
<link href=" {{ url_for('static', filename='css/color.css')  }}" rel="stylesheet">
<link href=" {{ url_for('static', filename='css/responsive.css')  }}" rel="stylesheet">
<link href=" {{ url_for('static', filename='css/dark-scheme.css')  }}" rel="stylesheet">

<script type="text/javascript" src=" {{ url_for('static', filename='script/jquery.js') }} "></script>

Solution

  • Based on the posted error and the project structure, I think that the problem is how you include responsive.css. Maybe the problem is your template.

    Look at this output.

    127.0.0.1 - - [30/Nov/2020 00:34:16] "GET /club/static/script/progressbar.js HTTP/1.1" 200
    

    And then at this.

    127.0.0.1 - - [30/Nov/2020 00:25:02] "GET /player/edit/static/css/responsive.css HTTP/1.1" 404
    

    The directory /static/script/ does indeed exist, but /edit/static/css/ does not as I can not find the edit directory. You probably mixed something up when you created the template, e.g. including a variable you did not intent. My guess is that there is an edit view for players, that is why it appears in the URL.

    Further, change this

    <link href="static/css/bootstrap.css" rel="stylesheet">
    <link href="static/css/font-awesome.css" rel="stylesheet">
    <link href="static/css/flaticon.css" rel="stylesheet">
    <link href="static/css/slick-slider.css" rel="stylesheet">
    <link href="static/css/fancybox.css" rel="stylesheet">
    <link href="static/style.css" rel="stylesheet">
    <link href="static/css/color.css" rel="stylesheet">
    <link href="static/css/responsive.css" rel="stylesheet">
    <link href="static/css/dark-scheme.css" rel="stylesheet">
    
    <script type="text/javascript" src="static/script/jquery.js"></script>
    

    to

    <link href=" {{ url_for('static', file_name='css/bootstrap') }} " rel="stylesheet">
    <link href=" {{ url_for('static', file_name='css/awesome.css') }}" rel="stylesheet">
    <link href=" {{ url_for('static', file_name='css/flaticon.css') }} " rel="stylesheet">
    <link href=" {{ url_for('static', file_name='css/slick-slider.css') }} " rel="stylesheet">
    <link href=" {{ url_for('static', file_name='css/fancybox.css') }} " rel="stylesheet">
    <link href=" {{ url_for('static', file_name='css/style.css') }} " rel="stylesheet">
    <link href=" {{ url_for('static', file_name='css/color.css') }} " rel="stylesheet">
    <link href=" {{ url_for('static', file_name='css/responsive.css') }} " rel="stylesheet">
    <link href=" {{ url_for('static', file_name='css/dark-scheme.css') }} " rel="stylesheet">
    
    <script type="text/javascript" src=" {{ url_for('static', file_name='script/jquery.js' }} "></script>
    

    HTH