Forums

Flask Blueprints works locally but breaks on pythonanywhere

Hi,

I have been banging my head against a wall now for hours and have been unable to find any directly relevant info online. Issue I'm facing is as described in the title. I'm using flask blueprints to better organise an existing project which is growing in size.

Behaviour on my local machine (Windows) and pythonanywhere are not the same:

Local - no issues the blueprint template is found and app works as expected

[2023-11-28 23:16:42,904] INFO in debughelpers: Locating template 'lrh/home/_index.jinja':    1: trying loader of application '__main__'
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - C:\Users\me\Code\workflows-redefined\src\templates
       -> no match
    2: trying loader of blueprint 'lrh' (lrh)
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - C:\Users\me\Code\workflows-redefined\src\lrh\templates
       -> found ('C:\\Users\\me\\Code\\workflows-redefined\\src\\lrh\\templates\\lrh\\home\\_index.jinja')
[2023-11-28 23:16:42,926] INFO in debughelpers: Locating template 'lrh/base.jinja ':
    1: trying loader of application '__main__'
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - C:\Users\me\Code\workflows-redefined\src\templates
       -> no match
    2: trying loader of blueprint 'lrh' (lrh)
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - C:\Users\me\Code\workflows-redefined\src\lrh\templates
       -> found ('C:\\Users\\me\\Code\\workflows-redefined\\src\\lrh\\templates\\lrh\\base.jinja ')
[2023-11-28 23:16:42,939] INFO in debughelpers: Locating template 'lrh/components/navbar.jinja':
    1: trying loader of application '__main__'
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - C:\Users\me\Code\workflows-redefined\src\templates
       -> no match
    2: trying loader of blueprint 'lrh' (lrh)
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - C:\Users\me\Code\workflows-redefined\src\lrh\templates
       -> found ('C:\\Users\\me\\Code\\workflows-redefined\\src\\lrh\\templates\\lrh\\components\\navbar.jinja')
[2023-11-28 23:16:42,946] INFO in debughelpers: Locating template 'lrh/components/footer.jinja':
    1: trying loader of application '__main__'
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - C:\Users\me\Code\workflows-redefined\src\templates
       -> no match
    2: trying loader of blueprint 'lrh' (lrh)
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - C:\Users\me\Code\workflows-redefined\src\lrh\templates
       -> found ('C:\\Users\\me\\Code\\workflows-redefined\\src\\lrh\\templates\\lrh\\components\\footer.jinja')

Pythonanywhere - same code, same file structure - not working. Finds a match for the template but is unable to extend the 'base.jinja' referenced within the blueprint:

2023-11-28 23:24:03,646: Locating template 'lrh/home/_index.jinja':
    1: trying loader of application 'main'
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /home/redefined/workflows/src/templates
       -> no match
    2: trying loader of blueprint 'lrh' (lrh)
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /home/redefined/workflows/src/lrh/templates
       -> found ('/home/redefined/workflows/src/lrh/templates/lrh/home/_index.jinja')
2023-11-28 23:24:03,656: Locating template 'lrh/base.jinja ':
    1: trying loader of application 'main'
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /home/redefined/workflows/src/templates
       -> no match
    2: trying loader of blueprint 'lrh' (lrh)
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /home/redefined/workflows/src/lrh/templates
       -> no match
Error: the template could not be found.
  The template was looked up from an endpoint that belongs to the blueprint 'lrh'.
  Maybe you did not place a template in the right folder?
  See https://flask.palletsprojects.com/blueprints/#templates
2023-11-28 23:24:03,659: Exception on /lrh/ [GET]
Traceback (most recent call last):
  File "/home/redefined/.virtualenvs/.venv_workflows/lib/python3.9/site-packages/flask/app.py", line 2190, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/redefined/.virtualenvs/.venv_workflows/lib/python3.9/site-packages/flask/app.py", line 1486, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/redefined/.virtualenvs/.venv_workflows/lib/python3.9/site-packages/flask/app.py", line 1484, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/redefined/.virtualenvs/.venv_workflows/lib/python3.9/site-packages/flask/app.py", line 1469, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/home/redefined/workflows/src/./lrh/__init__.py", line 8, in index
    return render_template(f"lrh/home/_index.jinja")
  File "/home/redefined/.virtualenvs/.venv_workflows/lib/python3.9/site-packages/flask/templating.py", line 151, in render_template
    return _render(app, template, context)
  File "/home/redefined/.virtualenvs/.venv_workflows/lib/python3.9/site-packages/flask/templating.py", line 132, in _render
    rv = template.render(context)
  File "/home/redefined/.virtualenvs/.venv_workflows/lib/python3.9/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/home/redefined/.virtualenvs/.venv_workflows/lib/python3.9/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "/home/redefined/workflows/src/lrh/templates/lrh/home/_index.jinja", line 1, in top-level template code
    {% extends 'lrh/base.jinja '%}
  File "/home/redefined/.virtualenvs/.venv_workflows/lib/python3.9/site-packages/flask/templating.py", line 63, in get_source
    return self._get_source_explained(environment, template)
  File "/home/redefined/.virtualenvs/.venv_workflows/lib/python3.9/site-packages/flask/templating.py", line 88, in _get_source_explained
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: lrh/base.jinja

For reference here is the blueprint code:

from flask import Blueprint, render_template

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

@lrh.route("/", methods=["GET"])
def index():
    return render_template(f"lrh/home/_index.jinja")

Any support would be much appreciated I've tried multiple ways of organising, using different filenames, referencing a different templates folder in the blueprint, using the main app templates folder and keep hitting this problem in every case.

Thanks, GD

What is the full path to the template you want to use?

Hi - it is /home/redefined/workflows/src/lrh/templates/lrh/base.jinja

The base template also {% includes %} the following:

/home/redefined/workflows/src/lrh/templates/lrh/components/navbar.jinja /home/redefined/workflows/src/lrh/templates/lrh/components/footer.jinja

The index template is:

/home/redefined/workflows/src/lrh/templates/lrh/home/_index.jinja

Thanks

It certainly looks like that should be working. Have you checked that you are using the same version of Flask in both places?

Hi - yes both are using Flask v.2.3.2 and Jinja2 v.3.1.2. Cheers.

Can we take a look at your files? We can see them from our admin interface, but we always ask for permission first.

Hi - yes that's fine. Thanks.

Could you remove trailing space from the path to the template string in {% extends 'lrh/base.jinja '%} and try again?

Not sure how I kept on missing that again and again :'( - it is up and working now. Thanks a lot!

Glad we could help!