Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to fix "Absolute template path names are deprecated."? #1429

Closed
FriedrichFroebel opened this issue Aug 11, 2023 · 5 comments
Closed

How to fix "Absolute template path names are deprecated."? #1429

FriedrichFroebel opened this issue Aug 11, 2023 · 5 comments

Comments

@FriedrichFroebel
Copy link

I am currently getting the following warning:

/usr/local/lib/python3.10/dist-packages/bottle.py:3383: DeprecationWarning: Absolute template path names are deprecated.
  fname = self.search(name, self.lookup)

This seems to be related to the fact that I explicitly set the TEMPLATE_PATH to ensure the template files are found when using a package structure (my template is in my_package/views/testing.jinja2):

from pathlib import Path

import bottle


CURRENT_DIRECTORY = Path(__file__).parent
bottle.TEMPLATE_PATH.append(CURRENT_DIRECTORY / 'views')

@bottle.route("/testing")
@bottle.view("testing.jinja2")
def testing():
    return {"key": 42}

The above code is referenced from the my_package/__main__.py file. Running python -m my_package from the parent directory of the package yields the above warning.

Without adding the new template path, browsing to the page yields error code 500 with

Template 'testing.jinja2' not found.

What is the correct way to resolve the templates when using a "real" package structure?

@simsong
Copy link

simsong commented Aug 27, 2023

try this and let me know?

import os.path
bottle.TEMPLATE_PATH.append( os.path.relpath( CURRENT_DIRECTORY / 'views'))

@FriedrichFroebel
Copy link
Author

This does not seem to work reliably and still raises the deprecation warning. bottle.TEMPLATE_PATH is ['./', './views/', 'brother_ql_web/views'].

It seems like bottle.Jinja2Template.loader resolves the name to /home/me/git/brother_ql_web/brother_ql_web/views/testing.jinja2 and thus

bottle/bottle.py

Line 3383 in 40aec5d

fname = self.search(name, self.lookup)
runs into

bottle/bottle.py

Lines 3283 to 3285 in 40aec5d

if os.path.isabs(name) and os.path.isfile(name):
depr('Absolute template path names are deprecated.') #0.12
return os.path.abspath(name)

@simsong
Copy link

simsong commented Aug 27, 2023

Oh, that makes sense. So jinja2 is turing it into an absolute path name and then bottle is complaining.
So we don't know why the author decided to depreciate this in 0.12, but that was probably a mistake. Note that the code just complains and then goes ahead and returns an absolute path!!! So I think that this is a bug in bottle.

@defnull
Copy link
Member

defnull commented Sep 5, 2024

The deprecation warning should only appear if you are including or extending other templates from within jinja2 using absolute paths. In short, {% include '/foo.j2' %} is bad but {% include 'foo.j2' %} should be fine. I tested it with 0.13-dev and it behaved as expected.

In case of an absolute path, the code in BaseTemplate.search bypassed the lookup path list, which is usually not what you want and deprecated behavior. In 0.13 this warning will be an error and trigger an exception.

@defnull defnull closed this as completed Sep 5, 2024
@FriedrichFroebel
Copy link
Author

I just did a quick test and it seems like I do not receive this warning any more with the development code. My extends calls where already using non-absolute paths.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants