我有一个包含模板和静态文件的烧瓶蓝图。模板呈现正确,但链接的CSS文件返回404 not found如何提供位于蓝图文件夹下的静态文件?

app/
  __init__.py
  auth/
    __init__.py
    static/
      style.css
    templates/
      index.html

auth = Blueprint('auth', __name__, template_folder='templates')

@auth.route('/')
def index():
    return render_template('index.html')

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

最佳答案

通过传递static_folder,告诉Blueprint其静态文件夹的位置,然后使用url_for生成指向Blueprint上static路由的URL。This is described in the docs.

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

url_for('auth.static', filename='style.css')

09-20 17:59