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

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.

Related

Template error while using Flask with blueprints

I am getting a template not found error while trying to run a flask app using blueprints. The template directory is located at the root directory as expected at the same level as the app directory. I am not very sure why this is happening.
The directory structure
root/
app/
blueprint1/
routes.py
__main__.py
templates/
base.html
index.html
routes.py
from flask import Blueprint, render_template
blueprint = Blueprint(
"blueprint", __name__, template_folder="templates")
#blueprint.route("/", methods=["GET", "POST"])
def index():
return render_template("index.html")
Error
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: index.html
main.py
from flask import Flask
from app.blueprint1.routes import blueprint
def create_app():
app = Flask(__name__)
app.register_blueprint(blueprint)
return app
def _main():
daemon_app = create_app()
daemon_app.run(debug=True)
if __name__ == "__main__":
_main()
This works, I edited it a little
routes.py
from flask import Blueprint, render_template
blueprint = Blueprint("blueprint", __name__, template_folder="../../templates")
#blueprint.route("/", methods=["GET", "POST"])
def index():
return render_template("index.html")
__main__.py
from flask import Flask
from routes import blueprint
def create_app():
app = Flask(__name__)
app.register_blueprint(blueprint)
return app
def _main():
daemon_app = create_app()
daemon_app.run(debug=True)
if __name__ == "__main__":
_main()`
Based on your file structure, your blueprint renders html files from the main template folder. It might work if you remove the template_folder argument from routes.py file.
I copied your codes and tried to emulate the error but it works for me. A year ago I have the same problem and somehow I solved it. Since I can't emulate the error I can't solve your problem.
Instead, I will just give you advise. You are making an effort to create blueprints in their own package, and yet you are using the main template folder. When you have a lot of blueprints managing the template folder will become a challenge. Thus, I recommend putting the html file within the blueprint package itself.
Here's the file structure:
root/
app/
blueprint1/
pages/blueprint1/index.html
routes.py
__main__.py
templates/
base.html
and routes.py should be like this
blueprint = Blueprint(
"blueprint", __name__, template_folder="pages")
#blueprint.route("/", methods=["GET", "POST"])
def index():
return render_template("blueprint1/index.html")

How do you route multiple apps in Flask Blueprints correctly?

I have just rearranged my flask site to support multiple apps, but seems to have issues with the blueprinting and routing with multiple apps. The below runs, but when you goto localhost/backstory, the index rendered is the home index as opposed to the backstory index. I tried doing a few things like using the prefix_url in the blueprint registration, but that doesn't seem to work.
From what I can see, the Blueprint() function is pointing to right directory and should reference the index in the right folder for the route /backstory. Yet, it's not doing that. What am I missing?
from flask import Flask
from database import database
# blueprint import
from apps.home.views import home_blueprint
from apps.backstory.views import backstory_blueprint
application = Flask(__name__)
# setup with the configuration provided
application.config.from_object('config.DevelopmentConfig')
# setup all our dependencies
database.init_app(application)
# register blueprint
application.register_blueprint(home_blueprint)
application.register_blueprint(backstory_blueprint)
if __name__ == "__main__":
application.run()
Home
from flask import Blueprint, request, url_for, redirect, render_template, flash
home_blueprint = Blueprint('home', __name__, template_folder="templates/home")
#home_blueprint.route("/")
def home():
return render_template('index.html')
Backstory
from flask import Blueprint, request, url_for, redirect, render_template, flash
backstory_blueprint = Blueprint('backstory', __name__, template_folder="templates/backstory")
#backstory_blueprint.route("/backstory")
def backstory():
return render_template('index.html')
Structure
Project
apps
backstory
templates
backstory
index.html
views.py
home
templates
home
index.html
views.py
application.py
I had the wrong templates_folder location it seems. Should have pointed it to the templates and then did backstory/index.html as the render_template() location per the documentation on Flask.
And then when you want to render the template, use admin/index.html as
the name to look up the template by. If you encounter problems loading
the correct templates enable the EXPLAIN_TEMPLATE_LOADING config
variable which will instruct Flask to print out the steps it goes
through to locate templates on every render_template call.
from flask import Blueprint, request, url_for, redirect, render_template, flash
backstory_blueprint = Blueprint('backstory', __name__, template_folder="templates")
#backstory_blueprint.route("/backstory")
def backstory():
return render_template('backstory/index.html')

flask bootstrap route 404 [duplicate]

This question already has an answer here:
Why use Flask's url_for?
(1 answer)
Closed 3 years ago.
im new to flask and boostrap,im trying to make a button that link to another html
Project layout
-Norman
-args
-static
-css
-bootstrap.min.css
-style.css
-img
-wall.jpg
-templates
-index.html
-form.html
-__init__.py
-routes.py
-.flaskenv
-readme.txt
-book.py
route.py:
from args import app # init.py
from flask import render_template
#app.route('/')
def index():
return render_template('index.html')
#app.route('/form')
def form():
return render_template('form.html')
__init__.py:
from flask import Flask
from flask_bootstrap import Bootstrap
app = Flask(__name__)
Bootstrap(app)
from args import routes
here is the code from index.html:
<div>
<button class="submit-btn">Check availability</button>
check form
</div>
im trying to link the button to open form.html with href but it says the requested url was not found on the server. opening localhost:5000 worked but the second route doesnt working
In your index.html: Replace /form.html to /form as you have registered the handle /form which when triggered, would render form.html
<div>
<button class="submit-btn">Check availability</button>
check form
</div>

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

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

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