0

I have a Flask blueprint packages structure like that:

application/
   -- __init__.py
   -- extensions.py
   -- authentication/
      -- templates/
         -- register.html
      -- __init__.py
      -- views.py
templates/
   -- base.html
static/
main.py
database.db

In my authentication/__init__.py:

from flask import Blueprint

authentication = Blueprint("authentication", __name__, template_folder="templates")

from . import views

In my authentication/views.py:

# REGISTER PAGE
@authentication.route('/register', methods=['GET', 'POST'])
def register():
    ...
    return render_template("register.html", form=form)

In my app/__init__.py:

def create_app():
    # INSTANTIATE APP
    app = Flask(__name__)
    ...
    app.register_blueprint(authentication, url_prefix="/")
    ...

Does someone knows what is wrong and why do I get jinja2.exceptions.TemplateNotFound: base.html on register page?

I have tried to do everything like stop and restart, putting base.html in the authentication.templates works but once it is in the root.templates it is written as not found.

Ces
  • 76
  • 1
  • 7

1 Answers1

1

Flask by default will only load templates from the root/templates folder. From the code you have provided your root folder is the /application folder so it is attempting to load the base.html template from /application/templates and not the /templates folder it currently exists at. You need to move the templates folder to /applications for it to work as currently implemented.

Setting the Blueprint parameter as listed below in your code will only set it to look for templates in /application/authentication/templates folder in addition to the default folder path /application/templates specified above:

template_folder="templates"

If you want to specify the folder one level above /application for the Blueprint you have to edit the Blueprint parameter as listed below:

template_folder='../../templates'

../ moves up one level of the file structure so you move two levels up arriving at where you originally had your base.html template listed. The reason for this is the path for the Blueprints is relative to the folder the Blueprint exists at which is two levels down from the templates/base.html folder in your code.

Aldo G.
  • 26
  • 3
  • Can you explain me a bit more what I did wrong ? Because I don't really understand the mistake here. Because I use blueprint packages, I have one template folder in the blueprint package and one in the root folder. So that is why I have this question : how to extend from root/template in a template located in blueprint/template – Ces Mar 14 '23 at 19:06
  • Its more a limitation with Flask not allowing two template folders so you have to choose if you want to use the default path or define your own path using the method linked to. Technical information on why can be found here: https://stackoverflow.com/questions/11192796/how-to-load-from-more-then-one-template-folder-for-flask-blueprint – Aldo G. Mar 14 '23 at 19:23
  • Yes but the thing is that in many different questions they say the exact opposite : https://stackoverflow.com/questions/30036273/access-a-layout-template-from-a-blueprint-template-folder for example. Another really strange thing I have realized is that once nothing is in the sub template folder, it locate the global template folder but once you have added a template in it, you can't go back and you will never be able to access the ancient folder again. Do you understand why ? I am really confused right now – Ces Mar 14 '23 at 19:37
  • The answer you linked to is referring to a feature of blueprints in which you can put your template files in the blueprint's "templates" directory or your main app's "templates" directory. You can choose which. The flattening it mentions is just that it combines the directories together. A technical explanation can be found in Flask's documentation under the heading "Templates": https://flask.palletsprojects.com/en/2.2.x/blueprints/ – Aldo G. Mar 14 '23 at 20:25
  • If you are just wanting to keep them separate you should move the templates directory into the application folder. – Aldo G. Mar 14 '23 at 20:39
  • Yes thank you that is what I was missing I was so confused because I thought that it was supposed to be at the same level, my bad thanks for your help ! – Ces Mar 14 '23 at 21:23
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 19 '23 at 20:51