Flask cannot find template file - jinja2.exceptions.TemplateNotFound [duplicate] - python

This question already has answers here:
Flask raises TemplateNotFound error even though template file exists
(14 answers)
Closed 5 years ago.
I tried to render an "html" file using Flask(Jinja2) in Python, but it showed me an error.
My controller ".py file":-
from flask import Flask, render_template
app = Flask(__name__)
#app.route("/profile/<name>")
def profile(name):
return render_template('profile.html', name=name)
if __name__ == "__main__":
app.run(debug=True)
My template "profile.html" file:
<h1>Welcome To Your Profile, {{ name }}</h1>
When I ran the flask app, it gave me the following exception:-
jinja2.exceptions.TemplateNotFound: template\profile.html

The error message says:
jinja2.exceptions.TemplateNotFound: template\profile.html
The templates folder should be named templates (in plural) and saved in the root path of the application. That is how Flask works by default.

Try this:
app = Flask(__name__, template_folder={path of template directory})

Related

jinja2.exceptions.TemplateNotFound: index.html error [duplicate]

This question already has answers here:
Flask raises TemplateNotFound error even though template file exists
(13 answers)
Closed 1 year ago.
I am trying to render the file index.html, but I keep getting
jinja2.exceptions.TemplateNotFound: index.html
when I try to render it. Why can't Flask find my template
app=Flask(__name__,template_folder='templates')
#app.route("/")
def index():
return render_template('index.html')
and my file location is like (i chance Tamplates to tamplates)
so you must define the template folder path like this.
if you named your template folder: Tamplates
Then use this
app = Flask(__name__, template_folder='Tamplates')

jinja2.exceptions.TemplateNotFound: index.html Render Error [duplicate]

This question already has answers here:
Flask raises TemplateNotFound error even though template file exists
(13 answers)
Closed 1 year ago.
when im trying to run the main.py i got TemplateNotFound: index.html
project file structure
app
templates : base.html, index.html
static
views.py
## viwes.py
from flask import render_template, request
from flask import redirect, url_for
def base():
return render_template('base.html')
def index():
return render_template('index.html')
def faceapp():
return render_template('faceapp.html')
##main.py
from flask import Flask
from app import views
app = Flask(__name__)
#### URL
app.add_url_rule('/base', 'base',views.base)
app.add_url_rule('/','index', views.index)
app.add_url_rule('/faceapp','faceapp', views.faceapp)
### RUN
if __name__ == "__main__":
app.run(debug=True)```
app = Flask(__name__, template_folder='path/to/templates/dir')
You can also explicitly define the path to the template directory as above.
You can also refer Flask Template for more details.

Flask can't find template when specifying templates folder [duplicate]

This question already has answers here:
Flask raises TemplateNotFound error even though template file exists
(14 answers)
How do I create a link to another html page?
(2 answers)
Closed 4 years ago.
I created a template in the templates folder next to my Flask app. When I try to render this template, I get a 404 error in the browser. Why can't I render the template?
from flask import Flask, render_template
app = Flask(__name__, template_folder='/templates')
#app.route('/')
def index():
return render_template('index.html')
You don't need to specify a template folder for the app, it's automatically set to 'templates'. As that default shows, it's a relative path.
You've provided an absolute path though, /templates is "the templates folder at the root of the filesystem". Your machine is unlikely to have a templates folder with your templates at the root.
Remove the template_folder parameter, or remove the leading /.

Setting URLS for REST API using Flask [duplicate]

This question already has answers here:
Flask raises TemplateNotFound error even though template file exists
(13 answers)
Closed 6 years ago.
I have two files - app.py which contains the code for a REST API and another page index.html which displays some contents of the API using AngularJS. Currently, I have to open the index.html file from the browser to get it working (after the server http://localhost:5000 is deployed).
I tried
#app.route('/')
def index():
return render_template('/index.html',
title='Home')
It is giving the error "TemplateNotFound: /index.html". Currently, app.py and index.html are in the same directory. How do I change the format to let Flask know that it has to render the page index.html on this URL.
In flask, templates should be located in a directory called templates
Your app should look like :
./app.py
./templates/index.html
http://flask.pocoo.org/docs/0.10/tutorial/templates/ :
Put the following templates into the templates folder
Try removing the slash from the template path. It is not necessary when the files are in the same directory.
return render_template('index.html',
title='Home')

Flask Template Not found [duplicate]

This question already has answers here:
Flask raises TemplateNotFound error even though template file exists
(13 answers)
Closed 7 years ago.
Implementing a simple static site from flask, but the browser says template not found, the shell returned 404
jinja2.exceptions.TemplateNotFound
TemplateNotFound: template.html
The main python code:
from flask import Flask, render_template
app = Flask(__name__)
#app.route("/")
def template_test():
return render_template('template.html', my_string="Wheeeee!", my_list=[0,1,2,3,4,5])
if __name__ == '__main__':
app.run(debug=True)
I have the following file structure:
flask_new_practice
|--template/
|--template.html
|--run.py
By default, Flask looks in the templates folder in the root level of your app.
http://flask.pocoo.org/docs/0.10/api/
template_folder – the folder that contains the templates that should
be used by the application. Defaults to 'templates' folder in the root
path of the application.
So you have some options,
rename template to templates
supply a template_folder param to have your template folder recognised by the flask app:
app = Flask(__name__, template_folder='template')

Categories

Resources