Flask raises TemplateNotFound error even though template file exists

前端 未结 11 1414
别跟我提以往
别跟我提以往 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:29

    You need to put all you .html files in the template folder next to your python module. And if there are any images that you are using in your html files then you need put all your files in the folder named static

    In the following Structure

    project/
        hello.py
        static/
            image.jpg
            style.css
        templates/
            homepage.html
        virtual/
            filename.json
    
    0 讨论(0)
  • 2020-11-21 23:32

    I think Flask uses the directory templates by default. So your code should be suppose this is your hello.py

    from flask import Flask,render_template
    
    app=Flask(__name__,template_folder='template')
    
    
    @app.route("/")
    def home():
        return render_template('home.html')
    
    @app.route("/about/")
    def about():
        return render_template('about.html')
    
    if __name__=="__main__":
        app.run(debug=True)
    

    And you work space structure like

    project/
        hello.py        
        template/
             home.html
             about.html    
        static/
               js/
                 main.js
               css/
                   main.css
    

    also you have create two html files with name of home.html and about.html and put those files in template folder.

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

    Check that:

    1. the template file has the right name
    2. the template file is in a subdirectory called templates
    3. the name you pass to render_template is relative to the template directory (index.html would be directly in the templates directory, auth/login.html would be under the auth directory in the templates directory.)
    4. you either do not have a subdirectory with the same name as your app, or the templates directory is inside that subdir.

    If that doesn't work, turn on debugging (app.debug = True) which might help figure out what's wrong.

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

    If you run your code from an installed package, make sure template files are present in directory <python root>/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.

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

    When render_template() function is used it tries to search for template in the folder called templates and it throws error jinja2.exceptions.TemplateNotFound when :

    1. the html file do not exist or
    2. when templates folder do not exist

    To solve the problem :

    create a folder with name templates in the same directory where the python file is located and place the html file created in the templates folder.

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