jinja2.exceptions.TemplateNotFound error using flask and python - python

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.

Related

jinja2.exceptions.TemplateNotFound: templates/index.html

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")

Why flask app run but browser is not loading the page?

I was developing a web application and it was woking fine, then I closed the project and re-opened it after a few hours, the project ran without error, but when I go to localhost:5000 it doesn't even load. I tried the same project in another laptop and it works perfectly.
I also tried a simple project in the problematic one like this. The program run, but the browser won't load the page, also here if I use my second laptop it works perfectly. What I should do to fix? Literally like 2 hours ago was working fine
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world(): # put application's code here
return 'Hello World!'
if __name__ == '__main__':
app.run()
My application code is:
from flask import Flask, render_template
from flask_login import current_user, LoginManager
from DaisPCTO.db import get_user_by_id
from flask_bootstrap import Bootstrap
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = "qwnfdopqwebnpqepfm"
Bootstrap(app)
login_manager = LoginManager()
login_manager.login_view = "auth_blueprint.login"
login_manager.init_app(app)
#app.route("/")
def home():
print("hello")
return render_template("page.html", user=current_user, roleProf = True if current_user.is_authenticated and current_user.hasRole("Professor") else False)
#login_manager.user_loader
def load_user(UserID):
return get_user_by_id(UserID)
from DaisPCTO.auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint)
from DaisPCTO.courses import courses as courses_blueprint
app.register_blueprint(courses_blueprint, url_prefix="/courses")
return app
i'm not putting all the blueprint, this is only the init.py file
In you application you don't run your app you just create a function
if __name__ == '__main__':
app = create_app()
app.run()
If you add this to your code you should be abble to see it in http://localhost:5000

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
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')

Get debug status out of Flask Script Manager

I'm using Flask-Script and I've got a manage.py that looks like this.
from mypackage import create_app
from flask.ext.script import Manager
app = create_app()
manager = Manager(app)
if __name__ == '__main__':
manager.run()
I'll start my app with python manage.py runserver --debug
From within manage.py, how can I find out that runserver was called with --debug?
I've tried app.debug but that returns False and manager.get_options() returns an empty list.
The code you've provided is fine-- here's a mypackage.py file to demonstrate that:
from flask import Flask, Blueprint, current_app
test = Blueprint('user', __name__, url_prefix='/')
#test.route('/')
def home():
return str(current_app.debug)
def create_app():
app = Flask(__name__)
app.register_blueprint(test)
return app
Adding --debug reflects accurately (True) when you access the index page.

Flask routes order matters?

I'm just started to play with Flask, so in all likelihood this is a seriously noobish question. This app is running on Google App Engine SDK 1.7.4. Flask 0.9, Werkzeug 0.9 and Jinja2 2.6.
The following code works as expected:
from flask import Flask
from flask import render_template
app = Flask(__name__)
#app.route('/')
def hello():
return "Main page"
#app.route('/hello/', methods=['GET', 'POST'])
#app.route('/hello/<name>', methods=['GET', 'POST'])
def hello(name=None):
return render_template('hello.html', name=name)
if __name__ == "__main__":
app.run()
However, if I reverse the route handlers, going to the /hello/ renders as if I went to /
from flask import Flask
from flask import render_template
app = Flask(__name__)
#app.route('/hello/', methods=['GET', 'POST'])
#app.route('/hello/<name>', methods=['GET', 'POST'])
def hello(name=None):
return render_template('hello.html', name=name)
#app.route('/')
def hello():
return "Main page"
if __name__ == "__main__":
app.run()
Worse yet, going to /hello/, for example /hello/John, results in error 500.
Is this normal behavior and the order of routes matters? If so, please also point me to relevant docs and, if possible, provide an explanation of why this order is so important.
You're creating two functions with the same name (hello). Rename the second one:
#app.route('/')
def index():
return "Main page"

Categories