Flask one 蓝图使用了错误的静态文件夹

Flask one Blueprint use wrong Static Folder

我目前正在使用 Flask 和蓝图编写 Web 应用程序。但是,蓝图使用了错误的静态文件夹,尽管配置应该是相同的。我的错误在哪里?

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")

只有 bp_player 蓝图没有找到静态文件夹:

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

我在根目录下只有一个静态文件夹。

编辑: 在根目录中是启动程序的 python 文件。 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>

适用于 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>

根据发布的错误和项目结构,我认为问题在于您如何包含 responsive.css。也许问题出在你的 template.

看看这个输出。

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

然后在这。

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

目录/static/script/确实存在,但是/edit/static/css/不存在,因为我找不到编辑目录。您在创建模板时可能混淆了一些东西,例如包括您无意的变量。我的猜测是玩家有一个 编辑视图 ,这就是它出现在 URL.

中的原因

进一步,改变这个

<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>

<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