I am facing this issue:
jinja2.exceptions.TemplateNotFound: templates/index.html
Although I have made templates folder and put index.html in it, but still I am getting this issue.
My directory structure:
/App
/templates
index.html
App.py
App.py looks like:
from flask import Flask, request, render_template
app = Flask(__name__, template_folder = 'templates')
#app.route("/")
def home():
return render_template("templates/index.html")
if __name__ == "__main__":
app.run()
If I make return statement in home() like this:
return "Hello World!"
It works fine.
Any help would be appreciated!
Try this:
return render_template("index.html")
Related
WHY IT DOES NOT SHOW WHAT I'VE WRITTEN IN HOME.HTML AND IT RESULTS TO THAT ERROR?
from flask import Flask, render_template
app = Flask(__name__)
#app.route("/")
def home_page():
return render_template('home.html')
app.debug = True
if __name__ == "__main__":
app.run()
You do have to specify the port in the app.run() call like app.run(port=8080, debug=True). Also check whether home.html is existing in the templates folder.
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__=="__main__":
app.run()
when I running the above code, I got the error "jinja2.exceptions.TemplateNotFound: index.html" evenif for simple hello world program. if anyone have solution please guide me.
Create a folder called templates which contains all HTML files and include this.
app = Flask(__name__, template_folder="templates", static_folder='static')
This error is caused when you attempt to return, via render_template an html file that cannot be found. They should be stored in a templates folder like this:
yourproject/
app.py
templates/
index.html
However your code makes no reference to either render_template or index.html. Why don't you update your question so that it applies to your error?
If you want to call your templates folder something other than templates then per another answer you could look to use something like this:
app = Flask(__name__, template_folder='MyPreferredTemplateFolderName')
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
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')
I'm trying to create a flask app using blueprints, so I have this structure:
myapp/
__init__.py
static/
templates/
site/
index.html
register.html
layout.html
views/
__init__.py
site.py
models.py
__init__.py
from flask import Flask
from .views.site import site
app = Flask(__name__)
app.register_blueprint(site)
views/site.py
from flask import Blueprint, render_template
site = Blueprint('site', __name__)
site.route('/')
def index():
return render_template('site/index.html')
site.route('/register/')
def register():
return render_template('site/register.html')
When I run the app, and try to go to any of the routes I have, the only thing I gest is a "404 Not Found" and I don't really know what I'm doing wrong, because I'm doing exactly what this book says: Explore Flask - Blueprints
Thanks in advance.
You have to prepend # to site.route like the following.
from flask import Blueprint, render_template
site = Blueprint('site', __name__)
#site.route('/')
def index():
return render_template('site/index.html')
#site.route('/register/')
def register():
return render_template('site/register.html')
I'm trying to use render_template in my Flask app:
# -*- coding: utf-8 -*-
from flask import Flask, render_template, redirect, url_for, request
app = Flask(__name__)
#app.route("/")
def hello_world():
return "Hello World", 200
#app.route('/welcome')
def welcome():
return render_template("welcome.html")
#app.route("/login", methods=["GET", "POST"])
def login():
error = None
if request.method == "POST":
if request.form["username"] != "admin" or request.form["password"] != "admin":
error = "Invalid credentials. Please try again!"
else:
return redirect(url_for("home"))
return render_template("login.html", error=error)
app.run(debug=True)
I think that my app can't see welcome.html (and the templates folder) because I always get a "Internal Server Error" and no HTML page.
In a single folder, I have app.py the static folder and the templates folder (with the HTML file).
What's wrong here?
Does your python script have all the necessary bits of a minimal Flask application?
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/welcome')
def welcome():
return render_template("/welcome.html")
if name== "__main__":
app.run()
You probably don't need a / in the name of the template.
(i.e. 'welcome.html', not '/welcome.html').
See the documentation for render_template.
It works with or without the slash.
Can you share the folder structure, and the script that you are executing (such as a init.py)
try looking at this (it's my repo for my webpage), to see if you might have missed something..