当'static'文件夹不在索引文件夹中时,如何使用 Flask 设置它?



我想建一个网站。我有索引文件夹和连接的文件夹。每个文件夹也有一个静态文件夹。在connected/static文件夹中,我有connected.css文件,我正试图通过我的蓝图访问该文件。但是,最后一页尝试从index/static文件夹访问connected.css

我哪里搞错了?

有用代码:

__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

连通路由.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">

在上面的行中,我有问题。

也许可以尝试一下:

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

详细说明如下:https://flask.palletsprojects.com/en/1.1.x/blueprints/#static-文件

相关内容

最新更新