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 /.
Related
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')
This question already has answers here:
How to serve static files in Flask
(24 answers)
Link to Flask static files with url_for
(2 answers)
Closed 4 years ago.
I'm trying to serve 2 static files named index.html and main.js in flask. My code is as follows:
from flask import Flask, url_for
app = Flask(__name__)
#app.route('/')
def host_html():
return url_for('static', filename='index.html')
#app.route('/map.js')
def host_map():
return url_for('static', filename='map.js')
if __name__ == '__main__':
app.run()
The output however on the web page is /static/index.html. What am I missing?
url_for function just returns a url, not the actual file. What you want to use in this case is send_from_directory: http://flask.pocoo.org/docs/1.0/api/#flask.send_from_directory
In general, i guess what you really want to do, is to use url_for in i a jinja template, which you return by render_template.
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})
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')
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')