Python 烧瓶-罐';无法访问根地址的静态文件

Python 烧瓶-罐';无法访问根地址的静态文件,python,flask,jinja2,static-files,blueprint,Python,Flask,Jinja2,Static Files,Blueprint,我正在使用Flask为自己创建一个个人测试网站,但是我发现了一个我无法完全理解的行为,可能需要一些帮助 我已经使用Flask的blueprint系统将我的网站分成了indivudial块,因为它对我的案例很有意义(因为我希望它包含多个较小的测试应用程序)。我怀疑我的问题根源于我的项目结构,所以我简要概述了我所做的工作。以下是我的(简化)项目设置: >File structure: root (contains some linux start scripts) - run.py -

我正在使用Flask为自己创建一个个人测试网站,但是我发现了一个我无法完全理解的行为,可能需要一些帮助

我已经使用Flask的blueprint系统将我的网站分成了indivudial块,因为它对我的案例很有意义(因为我希望它包含多个较小的测试应用程序)。我怀疑我的问题根源于我的项目结构,所以我简要概述了我所做的工作。以下是我的(简化)项目设置:

>File structure:
root (contains some linux start scripts)
  - run.py
  - website (the actual flask project folder)
      - __init__.py (registers blueprints)
      - blueprints
          - __init__.py (empty)
          - website
              - __init__.py (defines routes, creates blueprint)
              - static (static files for this blueprint)
                  - css
                      - example.css
              - templates (render templates for this blueprint)
                  - example.html.j2
          - app1
              - <Same structure as above>
          - app2
              - <Same structure as above>
          - ...

>run.py
from website import createApp
createApp().run(debug=True)


>website/__init__.py:
from flask import Flask, render_template

def createApp():
    app = Flask(__name__)
    app.testing = True

    # Website
    from blueprints.website import website
    app.register_blueprint(website())
    # App1
    from blueprints.app1 import app1
    app.register_blueprint(app1())
    # App2
    from blueprints.app2 import app2
    app.register_blueprint(app2())
    ...
    return app


>website/blueprints/website/__init__.py:
from flask import Blueprint, render_template
bp = Blueprint("website", __name__, url_prefix="/", 
template_folder="templates", static_folder="static")
def website():
    return bp

@bp.route('/')
def index():
    return render_template('example.html.j2')


>website/blueprints/website/templates/example.html.j2
<html>
  <head>
    <link rel="stylesheet", href="{{url_for('website.static', filename='css/example.css')}}">
    <title>Test Page!</title>
  </head>
  <body>
    This is a test page!
  </body>
</html>
>文件结构:
root(包含一些linux启动脚本)
-run.py
-网站(实际的烧瓶项目文件夹)
-_uuuinit_uuuuuu.py(注册蓝图)
-蓝图
-_uuuinit_uuuuuu.py(空)
-网站
-__init__u;.py(定义路线,创建蓝图)
-静态(此蓝图的静态文件)
-css
-example.css
-模板(此蓝图的渲染模板)
-example.html.j2
-附件1
- 
-附件2
- 
- ...
>run.py
从网站导入createApp
createApp().run(debug=True)
>网站/\uuuu init\uuuuu.py:
从烧瓶导入烧瓶,渲染\u模板
def createApp():
app=烧瓶(名称)
app.testing=True
#网站
从blueprints.website导入网站
应用程序注册蓝图(网站())
#附件1
从blueprints.app1导入app1
app.register\u蓝图(app1())
#附件2
从blueprints.app2导入app2
app.register\u蓝图(app2())
...
返回应用程序
>网站/blueprints/website/\uuuuuu init\uuuuuuuu.py:
从flask导入蓝图,渲染\u模板
bp=Blueprint(“网站”,名称,url前缀=“/”,
template\u folder=“templates”,static\u folder=“static”)
def网站():
返回bp
@bp.route(“/”)
def index():
返回render_模板('example.html.j2')
>网站/蓝图/网站/模板/example.html.j2
测试页面!
这是一个测试页面!
预期结果:页面应以示例中定义的样式显示。css
实际结果:加载示例.css文档会导致404错误

由于我已经尝试处理这个问题几个小时了,现在我想我已经把这个问题归结为当涉及到根地址时,Flask的怪异性

由于蓝图将地址定义为
url\u prefix=“/”
我通过在浏览器中键入“website.com”来访问它。(浏览器试图通过“website.com/static/css/example.css”调用资源,但得到404响应。)

如果我将地址更改为类似
url\u prefix=“/test”
,并通过“website.com/test”访问页面,样式表将成功加载。(浏览器现在尝试通过“website.com/test/static/css/example.css”调用资源,这次找到并加载文档。)

因为这应该是主页,所以我确实希望它使用根地址


如果有人能对此有所了解并向我解释我的错误所在,我将不胜感激。

有趣的问题。我唯一能想到的是,您可能已经指定
website.com/static
在WSGI服务器脚本中保存所有静态文件。因此,flask应用程序不会干扰对
website.com/static
的请求,这些请求由WSGI服务器处理,而WSGI服务器在文件夹中找不到这些请求

使用开发服务器时是否也会出现此问题

能否尝试将WSGI设置中的静态服务器更改为
website/blueprints/static/website
文件夹


最后,如果这没有帮助,你能用这个问题做一个小的github回购吗?这类导入和文件树很难重现这个问题

该环境已经是Flask Development服务器,它运行在朋友的Linux服务器上。我从来没有研究过WSGI,我需要一点时间来测试它,因为我需要做一点实验,我会带着结果回来。