Flask raises TemplateNotFound error even though template file exists

前端 未结 11 1426
别跟我提以往
别跟我提以往 2020-11-21 23:02

I am trying to render the file home.html. The file exists in my project, but I keep getting jinja2.exceptions.TemplateNotFound: home.html when I t

11条回答
  •  闹比i
    闹比i (楼主)
    2020-11-21 23:19

    Another alternative is to set the root_path which fixes the problem both for templates and static folders.

    root_path = Path(sys.executable).parent if getattr(sys, 'frozen', False) else Path(__file__).parent
    app = Flask(__name__.split('.')[0], root_path=root_path)
    

    If you render templates directly via Jinja2, then you write:

    ENV = jinja2.Environment(loader=jinja2.FileSystemLoader(str(root_path / 'templates')))
    template = ENV.get_template(your_template_name)
    

提交回复
热议问题