LoginSignup
3
1

More than 3 years have passed since last update.

jinja2のget_template()のパスをカレントディレクトリに依存させない

Posted at

パステンプレートファイルのパスが間違っているとこんなエラーが出る(検索用)

jinja2.exceptions.TemplateNotFound: xxxx.html
  • デフォルトではFileSystemLoaderに渡すパスをルートとして相対パスでファイルを検索するようになっている
    • 仮に'.'と指定するとカレントディレクトリからの相対パスになる
  • そこでこのコードを書くファイル自体の絶対パスをルートにすることによって実行場所、ファイルの配置場所を選ばなくなる
from jinja2 import Environment, FileSystemLoader
from os import  path
env = Environment(loader=FileSystemLoader(path.dirname(__file__)))
template = env.get_template('template.html')
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1