Flask raises TemplateNotFound error even though template file exists

前端 未结 11 1373
别跟我提以往
别跟我提以往 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: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)
    
    0 讨论(0)
  • 2020-11-21 23:21

    I don't know why, but I had to use the following folder structure instead. I put "templates" one level up.

    project/
        app/
            hello.py
            static/
                main.css
        templates/
            home.html
        venv/
    

    This probably indicates a misconfiguration elsewhere, but I couldn't figure out what that was and this worked.

    0 讨论(0)
  • 2020-11-21 23:25

    (Please note that the above accepted Answer provided for file/project structure is absolutely correct.)

    Also..

    In addition to properly setting up the project file structure, we have to tell flask to look in the appropriate level of the directory hierarchy.

    for example..

        app = Flask(__name__, template_folder='../templates')
    
        app = Flask(__name__, template_folder='../templates', static_folder='../static')
    

    Starting with ../ moves one directory backwards and starts there.

    Starting with ../../ moves two directories backwards and starts there (and so on...).

    Hope this helps

    0 讨论(0)
  • 2020-11-21 23:26

    My problem was that the file I was referencing from inside my home.html was a .j2 instead of a .html, and when I changed it back jinja could read it.

    Stupid error but it might help someone.

    0 讨论(0)
  • 2020-11-21 23:27

    You must create your template files in the correct location; in the templates subdirectory next to your python module.

    The error indicates that there is no home.html file in the templates/ directory. Make sure you created that directory in the same directory as your python module, and that you did in fact put a home.html file in that subdirectory. If your app is a package, the templates folder should be created inside the package.

    myproject/
        app.py
        templates/
            home.html
    
    myproject/
        mypackage/
            __init__.py
            templates/
                home.html
    

    Alternatively, if you named your templates folder something other than templates and don't want to rename it to the default, you can tell Flask to use that other directory.

    app = Flask(__name__, template_folder='template')  # still relative to module
    

    You can ask Flask to explain how it tried to find a given template, by setting the EXPLAIN_TEMPLATE_LOADING option to True. For every template loaded, you'll get a report logged to the Flask app.logger, at level INFO.

    This is what it looks like when a search is successful; in this example the foo/bar.html template extends the base.html template, so there are two searches:

    [2019-06-15 16:03:39,197] INFO in debughelpers: Locating template "foo/bar.html":
        1: trying loader of application "flaskpackagename"
           class: jinja2.loaders.FileSystemLoader
           encoding: 'utf-8'
           followlinks: False
           searchpath:
             - /.../project/flaskpackagename/templates
           -> found ('/.../project/flaskpackagename/templates/foo/bar.html')
    [2019-06-15 16:03:39,203] INFO in debughelpers: Locating template "base.html":
        1: trying loader of application "flaskpackagename"
           class: jinja2.loaders.FileSystemLoader
           encoding: 'utf-8'
           followlinks: False
           searchpath:
             - /.../project/flaskpackagename/templates
           -> found ('/.../project/flaskpackagename/templates/base.html')
    

    Blueprints can register their own template directories too, but this is not a requirement if you are using blueprints to make it easier to split a larger project across logical units. The main Flask app template directory is always searched first even when using additional paths per blueprint.

    0 讨论(0)
  • 2020-11-21 23:27

    I had the same error turns out the only thing i did wrong was to name my 'templates' folder,'template' without 's'. After changing that it worked fine,dont know why its a thing but it is.

    0 讨论(0)
提交回复
热议问题