模板文件夾templates
模板文件夾的是怎么確定的? 放到什么位置才能保證模板能被正確加載 / 或者訪問?
項(xiàng)目目錄
可以看出有兩個(gè)模板文件夾templates
News\info\templates\news\demo.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
我是頁面1,我在 E:\workspace\git\News\info\templates\news\demo.html
</body>
</html>
News\templates\news\demo.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
我是頁面2,我在 E:\workspace\git\News\templates\news\demo.html
</body>
</html>
main.py
from flask_migrate import MigrateCommand
from flask_script import Manager
from application.config import DevelopmentConfig
from info import create_app
app = create_app(DevelopmentConfig)
manager = Manager(app)
manager.add_command("db", MigrateCommand)
if __name__ == '__main__':
manager.run()
E:\workspace\git\News\info_init_.py
import redis
from flask import Flask
from flask_migrate import Migrate
from flask_session import Session
from flask_sqlalchemy import SQLAlchemy
from application.config import Config
db_SQLAlchemy = SQLAlchemy()
redis_store = None # type:redis.StrictRedis
def create_app(config_name: Config):
global redis_store
# 重點(diǎn)在這 __name__
app = Flask(__name__,
instance_path="E:\\workspace\\git\\News\\config_private",
instance_relative_config=True)
app.config.from_pyfile("config_private.py")
app.config.from_object(app.config["CONFIGPRIVATE"])
app.config.from_object(config_name)
redis_store = redis.StrictRedis(host=app.config["CONFIGPRIVATE"].REDIS_HOST,
port=app.config["CONFIGPRIVATE"],
db=app.config["CONFIGPRIVATE"].REDIS_DB)
db_SQLAlchemy.init_app(app)
Session(app)
Migrate(app, db_SQLAlchemy)
from info.modules.index import index_blue
app.register_blueprint(index_blue)
return app
if __name__ == '__main__':
create_app(Config)
views.py
from flask import render_template
from . import index_blue
@index_blue.route('/')
def index():
print("index")
return render_template("news/index.html")
@index_blue.route('/demo')
def demo():
print("demo")
return render_template("news/demo.html")
如果我要加載頁面是加載demo.html , 程序是加載頁面1 還是頁面2 ?
運(yùn)行結(jié)果
那么問題來了, 為什么是頁面1 不是頁面2? Flask初探一(Flask 各參數(shù)的應(yīng)用) 中介紹了各參數(shù)的作用, 其中template_folder 就是模板文件的文件夾, 要想清楚為什么是頁面1 不是頁面2 , 就要先弄清楚template_folder 文件夾的路徑問題
官方注釋
:param template_folder: the folder that contains the templates that should
be used by the application. Defaults to
'templates'
folder in the root path of the
application.
可以從注釋得出一個(gè)結(jié)論, templates 路徑和 root path 有關(guān), 那么 Flask初探一(Flask 各參數(shù)的應(yīng)用) 中得到一個(gè)結(jié)論
root_path: 默認(rèn)情況下,flask將自動(dòng)計(jì)算引用程序根的絕對(duì)路徑, 由import_name 決定.
所以可以從import_name 出發(fā), 研究加載template_folder 的路徑
默認(rèn)情況
E:\workspace\git\News\info_init_.py
import redis
from flask import Flask
from flask_migrate import Migrate
from flask_session import Session
from flask_sqlalchemy import SQLAlchemy
from application.config import Config
db_SQLAlchemy = SQLAlchemy()
redis_store = None # type:redis.StrictRedis
def create_app(config_name: Config):
global redis_store
# 重點(diǎn) import_name 為 __name__
app = Flask(__name__,
instance_path="E:\\workspace\\git\\News\\config_private",
instance_relative_config=True)
app.config.from_pyfile("config_private.py")
app.config.from_object(app.config["CONFIGPRIVATE"])
app.config.from_object(config_name)
redis_store = redis.StrictRedis(host=app.config["CONFIGPRIVATE"].REDIS_HOST,
port=app.config["CONFIGPRIVATE"],
db=app.config["CONFIGPRIVATE"].REDIS_DB)
db_SQLAlchemy.init_app(app)
Session(app)
Migrate(app, db_SQLAlchemy)
from info.modules.index import index_blue
app.register_blueprint(index_blue)
return app
if __name__ == '__main__':
create_app(Config)
刪除demo.html,從報(bào)錯(cuò)信息查找路徑
報(bào)錯(cuò)信息
Traceback (most recent call last):
File "D:\software\Python\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "D:\software\Python\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "D:\software\Python\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "D:\software\Python\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "D:\software\Python\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "D:\software\Python\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "D:\software\Python\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "D:\software\Python\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "D:\software\Python\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "D:\software\Python\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "E:\workspace\git\News\info\modules\index\views.py", line 15, in demo
return render_template("news/demo.html")
File "D:\software\Python\lib\site-packages\flask\templating.py", line 127, in render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "D:\software\Python\lib\site-packages\jinja2\environment.py", line 869, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "D:\software\Python\lib\site-packages\jinja2\environment.py", line 830, in get_template
return self._load_template(name, self.make_globals(globals))
File "D:\software\Python\lib\site-packages\jinja2\environment.py", line 804, in _load_template
template = self.loader.load(self, name, globals)
File "D:\software\Python\lib\site-packages\jinja2\loaders.py", line 113, in load
source, filename, uptodate = self.get_source(environment, name)
File "D:\software\Python\lib\site-packages\flask\templating.py", line 64, in get_source
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: news/demo.html
看這里
File "D:\software\Python\lib\site-packages\jinja2\loaders.py", line 113, in load
source, filename, uptodate = self.get_source(environment, name)
執(zhí)行完這一句之后報(bào)錯(cuò)了, 那么我們?cè)谶@里斷點(diǎn)查看一下
- self.get_source(environment, name) 是一個(gè)方法,進(jìn)入
- 進(jìn)入之后可以看到如下方法
def get_source(self, environment, template):
for loader, local_name in self._iter_loaders(template):
try:
return loader.get_source(environment, local_name)
except TemplateNotFound:
pass
- 進(jìn)入 loader.get_source(environment, local_name) 方法
def get_source(self, environment, template):
pieces = split_template_path(template)
for searchpath in self.searchpath:
filename = path.join(searchpath, *pieces)
f = open_if_exists(filename)
if f is None:
continue
try:
contents = f.read().decode(self.encoding)
finally:
f.close()
mtime = path.getmtime(filename)
def uptodate():
try:
return path.getmtime(filename) == mtime
except OSError:
return False
return contents, filename, uptodate
raise TemplateNotFound(template)
在我的項(xiàng)目中, 默認(rèn)情況下是在 'E:\workspace\git\News\info\templates' 路徑下搜索 templates 模板文件夾, 為什么呢?
因?yàn)閞oot_path 是根據(jù)import_name 由flask 自動(dòng)計(jì)算出的路徑, 如果想改變 templates 模板文件夾的搜索路徑, 只需要改變
import_name 就可以實(shí)現(xiàn).
驗(yàn)證
上面我們推測出一個(gè)結(jié)論
如果想改變 templates 模板文件夾的搜索路徑, 只需要改變import_name 就可以實(shí)現(xiàn).
因?yàn)?E:\workspace\git\News\templates\news\demo.html 在項(xiàng)目的目錄的根目錄下, 所以要將root_path 的路徑改為E:\workspace\git\News ,這時(shí) templates 模板文件夾的搜索路徑才可能是 E:\workspace\git\News\templates, 假設(shè)
import_name 等于"News" ,既項(xiàng)目的根目錄文件夾, 查看root_path 以及 模板文件夾的搜索路徑searchpath 的變化
實(shí)驗(yàn)
E:\workspace\git\News\info_init_.py
import redis
from flask import Flask
from flask_migrate import Migrate
from flask_session import Session
from flask_sqlalchemy import SQLAlchemy
from application.config import Config
db_SQLAlchemy = SQLAlchemy()
redis_store = None # type:redis.StrictRedis
def create_app(config_name: Config):
global redis_store
# 讓import_name 等于"News"
app = Flask("News",
instance_path="E:\\workspace\\git\\News\\config_private",
instance_relative_config=True)
app.config.from_pyfile("config_private.py")
app.config.from_object(app.config["CONFIGPRIVATE"])
app.config.from_object(config_name)
redis_store = redis.StrictRedis(host=app.config["CONFIGPRIVATE"].REDIS_HOST,
port=app.config["CONFIGPRIVATE"],
db=app.config["CONFIGPRIVATE"].REDIS_DB)
db_SQLAlchemy.init_app(app)
Session(app)
Migrate(app, db_SQLAlchemy)
from info.modules.index import index_blue
app.register_blueprint(index_blue)
return app
if __name__ == '__main__':
create_app(Config)
運(yùn)行結(jié)果
root_path 的值
searchpath 的值
頁面顯示
通過以上結(jié)果證實(shí)確實(shí)可以通過改變 import_name 進(jìn)而改變模板文件夾的搜索路徑. 那么是怎么影響的?這個(gè)問題不得不從import_name 是如何得到root_path開始回答.
在Flask初探一 已經(jīng)知道import_name 和 root_path 的關(guān)系這里在進(jìn)一步研究一下
get_root_path
def get_root_path(import_name):
"""Returns the path to a package or cwd if that cannot be found. This
returns the path of a package or the folder that contains a module.
Not to be confused with the package path returned by :func:`find_package`.
"""
# Module already imported and has a file attribute. Use that first.
mod = sys.modules.get(import_name)
if mod is not None and hasattr(mod, '__file__'):
return os.path.dirname(os.path.abspath(mod.__file__))
# Next attempt: check the loader.
loader = pkgutil.get_loader(import_name)
# Loader does not exist or we're referring to an unloaded main module
# or a main module without path (interactive sessions), go with the
# current working directory.
if loader is None or import_name == '__main__':
return os.getcwd()
# For .egg, zipimporter does not have get_filename until Python 2.7.
# Some other loaders might exhibit the same behavior.
if hasattr(loader, 'get_filename'):
filepath = loader.get_filename(import_name)
else:
# Fall back to imports.
__import__(import_name)
filepath = sys.modules[import_name].__file__
# filepath is import_name.py for a module, or __init__.py for a package.
return os.path.dirname(os.path.abspath(filepath))
通過get_root_path 方法傳入import_name 得到root_path.
這個(gè)方法大致分為三個(gè)部分 Module already / check the loader / other loaders, 在前面文章中研究了Module already,這里研究check the loader的部分,
既下面的代碼
# Next attempt: check the loader.
loader = pkgutil.get_loader(import_name)
# Loader does not exist or we're referring to an unloaded main module
# or a main module without path (interactive sessions), go with the
# current working directory.
if loader is None or import_name == '__main__':
return os.getcwd()
通過這里可以看出當(dāng) loader is None 或者 import_name == 'main': 時(shí), 將 當(dāng)前的工作目錄 ,在我當(dāng)前的項(xiàng)目既是E:\workspace\git\News 這個(gè)路徑. 所以可以推測模板搜索路徑是在roo_path 的下級(jí)目錄尋找templates 模板文件夾, 進(jìn)而尋找模板文件.
到此結(jié)? DragonFangQy 2018.6.25