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.
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.