Flask raises TemplateNotFound error even though template file exists

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

提交回复
热议问题