问题描述
项目目录下如下:
MyApplication
	__init__.py
    run.py
    app/
        admin/
            __init__.py
            view.py
            templates/
                index.html
        user/
            __init__.py
            view.py
            templates/
                index.html
  admin/view.py 的内容如下:
from flask import render_template
admin = Blueprint('admin', __name__, template_folder='templates')
@admin.route('/')
def index():
    return render_template('index.html')
  user/view.py 的内容如下:
from flask import render_template
user = Blueprint('user', __name__, template_folder='templates')
@user.route('/')
def index():
    return render_template('index.html')
  此时,如果访问 http://127.0.0.1:5000/admin,将正常访问 admin\templates\index.html。如果访问 http://127.0.0.1:5000/user,仍显示 admin\templates\index.html
原因
- app 创建时会从 Flask注册函数中读取 
templates_folder,如果没有设置,默认是app/templates作为全局jinja_loader render_template函数会首先访问 app 的全局jinja_loader,从中读取模板路径- 访问不到就会循环访问所有注册蓝图的 jinja_loader。多个蓝图提供相同的相对路径时,第一个注册的优先
 
解决思路
-  
嵌套目录
 -  
合并目录
3. 参考 

京公网安备 11010502036488号