FLask初探四 ( 確定項目模板的加載路徑)

模板文件夾templates

模板文件夾的是怎么確定的? 放到什么位置才能保證模板能被正確加載 / 或者訪問?

項目目錄

01-測試目錄結構.png

可以看出有兩個模板文件夾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

    # 重點在這 __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 ?

運行結果

02-運行結果.png

那么問題來了, 為什么是頁面1 不是頁面2? Flask初探一(Flask 各參數的應用) 中介紹了各參數的作用, 其中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.

可以從注釋得出一個結論, templates 路徑和 root path 有關, 那么 Flask初探一(Flask 各參數的應用) 中得到一個結論

root_path: 默認情況下,flask將自動計算引用程序根的絕對路徑, 由import_name 決定.

所以可以從import_name 出發, 研究加載template_folder 的路徑

默認情況

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 為 __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,從報錯信息查找路徑

報錯信息

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)

執行完這一句之后報錯了, 那么我們在這里斷點查看一下

  • self.get_source(environment, name) 是一個方法,進入
  • 進入之后可以看到如下方法
 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
  • 進入 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)

03-模板的默認搜索路徑.png

在我的項目中, 默認情況下是在 'E:\workspace\git\News\info\templates' 路徑下搜索 templates 模板文件夾, 為什么呢?
因為root_path 是根據import_name 由flask 自動計算出的路徑, 如果想改變 templates 模板文件夾的搜索路徑, 只需要改變
import_name 就可以實現.

驗證

上面我們推測出一個結論

如果想改變 templates 模板文件夾的搜索路徑, 只需要改變import_name 就可以實現.

因為 E:\workspace\git\News\templates\news\demo.html 在項目的目錄的根目錄下, 所以要將root_path 的路徑改為E:\workspace\git\News ,這時 templates 模板文件夾的搜索路徑才可能是 E:\workspace\git\News\templates, 假設
import_name 等于"News" ,既項目的根目錄文件夾, 查看root_path 以及 模板文件夾的搜索路徑searchpath 的變化

實驗

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)

運行結果

root_path 的值

04-root_path 的值.png

searchpath 的值

05-searchpath 的值.png

頁面顯示

06-頁面顯示.png

通過以上結果證實確實可以通過改變 import_name 進而改變模板文件夾的搜索路徑. 那么是怎么影響的?這個問題不得不從import_name 是如何得到root_path開始回答.

Flask初探一 已經知道import_name 和 root_path 的關系這里在進一步研究一下

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.
這個方法大致分為三個部分 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()

通過這里可以看出當 loader is None 或者 import_name == 'main': 時, 將 當前的工作目錄 ,在我當前的項目既是E:\workspace\git\News 這個路徑. 所以可以推測模板搜索路徑是在roo_path 的下級目錄尋找templates 模板文件夾, 進而尋找模板文件.


到此結? DragonFangQy 2018.6.25

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容