I have the following code structure:
app/
home/
__init__.py
routes.py
templates/
home
index.html
static
templates/
layout.html
todo/
__init__.py
routes.py
templates/
todo/
list.html
update.html
__init__.py
config.py
models.py
In the __init__.py
file I have the following:
from flask import Blueprint
todo = Blueprint('todo', __name__, template_folder='templates')
from app.todo import routes
The routes.py
file contains:
from flask import request, render_template, redirect
from app.todo import todo
from ..models import Task, db
@todo.route('/todos')
def home():
tasks = Task.query.order_by(Task.created_at).all()
return render_template("todo/list.html", tasks=tasks)
The todo/templates/todo/list.html
file contains the following code:
{% extends "layout.html" %}
{% block content %}
....
The layout.html file is in the app/templates
folder.
My app/__init__.py
file has the following:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from app.config import app_config
db = SQLAlchemy()
def init_app(app_name, config_name):
app = Flask(app_name)
app.config.from_object(app_config[config_name])
db.init_app(app)
with app.app_context():
from .home.routes import home
from .todo.routes import todo
app.register_blueprint(home)
app.register_blueprint(todo)
return app
When I accesss the app via http://localhost:5000/todos, I get following message:
jinja2.exceptions.TemplateNotFound: layout.html
I understood that Flask would search first in the templates folder under app so I don't quite understand why it fails to find this template layout.html
.