0

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.

wiwa1978
  • 2,317
  • 3
  • 31
  • 67
  • Does this answer your question? [Flask raises TemplateNotFound error even though template file exists](https://stackoverflow.com/questions/23327293/flask-raises-templatenotfound-error-even-though-template-file-exists) – rfkortekaas Dec 28 '20 at 08:49
  • No, that post handles with templates usages in Blueprint. That part works for me. When I show a template that is not extending other layout, then everything works fine. It's specifically when I extend the layout file in a template that it complains that it does not find the layout file (from which I'm extending). – wiwa1978 Dec 28 '20 at 08:58
  • Have a look at the accepted answer and especially the option http://flask.pocoo.org/docs/1.0/config/#EXPLAIN_TEMPLATE_LOADING probably this shows why it cannot find the layout.html – rfkortekaas Dec 28 '20 at 09:02
  • 1
    Thanks. Looked at the post in more detail. Based on these hints I found the result. I added it as a separate answer. – wiwa1978 Dec 28 '20 at 10:31

1 Answers1

1

The issue is related to the place of the templates directory. This should be under the root project, not under the app folder.

app/
   home/
      __init__.py
      routes.py
      templates/
         home
         index.html
   todo/
      __init__.py
      routes.py
      templates/
         todo/
           list.html
           update.html
    __init__.py
    config.py
    models.py
    static
    templates/
        layout.html

In this case, Flask will find the layout.html template.

wiwa1978
  • 2,317
  • 3
  • 31
  • 67