Flask raises TemplateNotFound error even though template file exists

前端 未结 11 1559
别跟我提以往
别跟我提以往 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条回答
  •  半阙折子戏
    2020-11-21 23:34

    If you run your code from an installed package, make sure template files are present in directory /lib/site-packages/your-package/templates.


    Some details:

    In my case I was trying to run examples of project flask_simple_ui and jinja would always say

    jinja2.exceptions.TemplateNotFound: form.html

    The trick was that sample program would import installed package flask_simple_ui. And ninja being used from inside that package is using as root directory for lookup the package path, in my case ...python/lib/site-packages/flask_simple_ui, instead of os.getcwd() as one would expect.

    To my bad luck, setup.py has a bug and doesn't copy any html files, including the missing form.html. Once I fixed setup.py, the problem with TemplateNotFound vanished.

    I hope it helps someone.

提交回复
热议问题