Veztar
Veztar

Reputation: 39

Flask TemplateNotFound

I am new to flask and try to run a very simple python file which calls an HTML document, but whenever I search on http://127.0.0.1:5000/, it raises the TemplateNotFound error. I searched stack overflow on similar questions, but even with implementing the solutions, I get the same error. Nothing worked so far

base.html contains:

<body>
    <h1>Hello there</h1>
</body>
</html>

flask_test_2.py contains:

from flask import Flask, render_template
app = Flask(__name__, template_folder='templates')

@app.route('/')
def index():
   return render_template('base.html')
   
if __name__ == '__main__':
    app.run()

as advised in some of the solutions, I checked the file structure:

/flask_test_2.py
/templates
    /base.html

Upvotes: 0

Views: 424

Answers (1)

Seyi Daniel
Seyi Daniel

Reputation: 2379

It should work. Since is doesn't, you may take out the template_folder='templates' portion of you app` assignment and have it this way:

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def index():
   return render_template('base.html')

if __name__ == '__main__':
    app.run()

it will use the templates directory by default.

The issue you are experiencing may be path related. You may also declare the app variable this way:

app = Flask(__name__, template_folder='usr\...\templates')

Upvotes: 1

Related Questions