本文主要是记录使用Flask过程中遇到的问题。本章主要讨论render_template函数。使用Flask的同学应该都知道,项目中的url和view函数在字典中是一一对应的。更详细一点,url对应端点,视图函数也对应端点,端点在字典中是唯一的。的。对于不同Blueprints中的url,通过注册的Blueprints和不同的前缀来区分。但是,端点不支持在视图函数中调用的render_template函数。如果在不同目录下使用同名模板文件,就会出现问题。先有事实,后有推理。示例工程的目录结构如下:app├──admin│├──errors.py│├──forms.py│├──__init__.py│├──static││├──css│├──模板││├──index.html│├──views.py├──__init__.py├──main│├──errors.py│├──forms.py│├──__init__.py│├──templates││└──ousi││├──index.html││├──static││││├──css│├──views.py├──models.py在此注册两个蓝图project,也就是ad??min就是所谓的后台管理蓝图,main就是所谓的前端展示蓝图。blueprintadmin的__init__.py内容如下:#-*-coding:utf-8-*-__author__='OrientalOsprey'fromflaskimportBlueprintadmin=Blueprint('admin',__name__,template_folder="templates",static_folder='static')#在最后导入相关模块,避免循环导入依赖,因为蓝图mainfrom.importviews,errorsblueprintmain的__init__.py内容如下:#-*-coding:utf-8-*-__author__='OrientalOsprey'fromflaskimportBlueprintmain=Blueprint('main',__name__,template_folder="templates/ousi",static_folder='templates/ousi/static')#导入相关模块在最后,是为了避免循环导入依赖,因为蓝图mainfrom.importviews,errors也导入了下面的模块。在定义两个蓝图的时候,同时定义了这个蓝图对应的template文件夹和static文件夹。本文主要关注templates文件夹。所以,现在出了什么问题。主页“/”或“索引”在相应蓝图的视图函数中定义。其中,蓝图admin的视图函数定义如下:@admin.route('/',methods=['GET','POST'])@login_requireddefindex():returnrender_template('index.html')请记住最后一段代码,也就是render_template('index.html'),这里调用的模板的名字叫index.html。blueprintadmin的视图函数定义如下:@main.route('/',methods=['GET','POST'])defindex():returnrender_template('index.html')请记住最后一段代码,即render_template('index.html'),这里调用的模板名称为index.html。到目前为止,你发现了什么,你发现了什么,我猜你已经看到两个视图函数的最后一行代码是一样的,更准确的说,调用的模板名称是一样的。但在这里我们需要清醒。虽然模板名称相同,但目录不同。它们位于各自蓝图定义的模板文件夹中。说了这么多,到底发生了什么?这时候你测试一下你的程序,你会发现两个蓝图显示的内容是一样的,不管你信不信,都是一样的界面,绝对一样,因为render_template('index.html')调用相同的模板,它不区分蓝图。那么,蓝图下的模板叫什么??继续阅读。此时你打开app/__init__.py,内容如下:defcreate_app(config_name):"""使用工厂函数初始化程序实例"""app=Flask(__name__)app.config.from_object(config[config_name])config[config_name].init_app(app=app)#mail.init_app(app=app)moment.init_app(app=app)db.init_app(app=app)md.init_app(app=app)login_manager.init_app(app=app)#Registerblueprintmainfrom.mainimportmainasmain_blueprintapp.register_blueprint(main_blueprint,url_prefix='/main')#Registerblueprintadminfrom.adminimportadminasadmin_blueprintapp.register_blueprint(admin_blueprint,url_prefix='/admin')#RegisterBlueprintmain#from.mainimportmainasmain_blueprint#app.register_blueprint(main_blueprint,url_prefix='/dynamic')returnApp到底调用了蓝图下的模板??这个,我可以明确的告诉你,main和admin这两个蓝图,在app/__init__.py里面先注册的那个,会调用那个蓝图的模板,也就是写哪个蓝图注册的代码更高,然后为该蓝图调用A模板。这到底是为什么呢?为什么?这是flask项目的一个小bug。写项目的时候要注意这个坑。render_template()函数中调用的模板必须保证名称在整个项目中的唯一性。号外,号外今天又发现了一个坑,就是jinja2里面的模板调用。比如在包含某个模板的时候,被调用模板的名字也是需要的,强烈需要保证在整个项目中,记住在整个项目中,其他的命名应该是唯一的。
