简体   繁体   中英

render_template() cannot find template located in blueprint folder

The following is a part of my setup in a flask application, using Flask-Blueprints, where reports is a blueprint:

app
├── __init__.py
└── /reports
    ├── /templates
    │   └── /reports
    │       └── reports.html
    ├── __init__.py
    └── views.py

This is my app/reports/__init.py :

from flask import Blueprint

reports_bp = Blueprint('reports', __name__)

from app.reports import views

And this is how I register the reports blueprint within app/__init__.py (notice especially how I specify the location of my template folder as 'templates' ):

...
from app.reports import reports_bp
app.register_blueprint(reports_bp, url_prefix='/reports',  template_folder='templates')
...

Finally I have a view function in app/reports/views.py that looks something like this:

@reports_bp.route('/')
def reports():

    ...

    return render_template('reports/reports.html', ...)

render_template however doesn't find my reports.html template, and when I enable the EXPLAIN_TEMPLATE_LOADING config variable I get this feedback:

[2020-03-11 14:40:21,966] INFO in debughelpers: Locating template "reports/reports.html":
    1: trying loader of application "app"
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /path_to_my_app/app/templates
       -> no match
    2: trying loader of blueprint "restplus_doc" (flask_restplus.apidoc)
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /path_to_my_app/my_virtualenv/lib/python3.7/site-packages/flask_restplus/templates
       -> no match
Error: the template could not be found.
  The template was looked up from an endpoint that belongs to the blueprint "reports".
  Maybe you did not place a template in the right folder?
  See http://flask.pocoo.org/docs/blueprints/#templates

The first searchpath is to be expected, but why is the second searchpath from within a site-package folder inside my virtualenv, to which the postfix /templates is uselessly attached? Shouldn't the second searchpath be the blueprint folder reports ?

I am using VS Code and running this in debugger mode. My installed Flask is version 1.0.2.

EDIT: Changing the template_folder when registering the blueprint to something else, like 'reports/templates' doesn't change searchpath number 2. Which gives me the hint that some other blueprint is bullying my blueprint and controlling where render_template searches.

EDIT 2: The real problem here was that a loader was not created for my blueprint. Loader nr.2 belongs to another blueprint I was unaware of when posting the question and is not relevant to my issue. See posted answer for more details.

Apparantly, the template_folder='templates' bit is supposed to be included as a parameter when the blueprint is initialized instead of when it is registered like I had been doing.

So now I initialize the blueprint like this in app/reports/__init.py :

from flask import Blueprint

reports_bp = Blueprint('reports', __name__, template_folder='templates')

from app.reports import views

and I no longer need to specify anything about the template folder when registering the blueprint, see my updated bit of code from app/__init__.py :

...
from app.reports import reports_bp
app.register_blueprint(reports_bp, url_prefix='/reports')
...

Just to show that there has now been added an new jinja loader, this is now printed when the template is rendered:

[2020-03-12 13:19:54,870] INFO in debughelpers: Locating template "reports/reports.html":
    1: trying loader of application "app"
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /path_to_my_app/app/templates
       -> no match
    2: trying loader of blueprint "restplus_doc" (flask_restplus.apidoc)
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /path_to_my_app/ankappenv3/lib/python3.7/site-packages/flask_restplus/templates
       -> no match
    3: trying loader of blueprint "reports" (app.reports)
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /path_to_my_app/app/reports/templates
       -> found ('/path_to_my_app/app/reports/templates/reports/reports.html')

Here you can see that a third loader has been added, and that one searches the specified /app/reports/templates/ path correctly, where it finds the template.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM