-
Notifications
You must be signed in to change notification settings - Fork 16
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
Try to re-use settings.py template loader config before using defaults #31
base: main
Are you sure you want to change the base?
Conversation
We'd need a regression test here. |
For sure, just added one modeling after the other SimpleAppConfig test. Let me know if that's sufficient. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @ekaj2.
So I don't think this works for the general case.
Especially when using docker, folks often disable the cached loader in development.
That gives them a config that looks like this:
"OPTIONS": {
"loaders":[
"django.template.loaders.filesystem.Loader",
"django.template.loaders.app_directories.Loader",
"custom.custom_loader.Loader",
],
},
We can't then assume that the cached loader is wanted.
Can I ask you to add a test with this kind of config?
If I'm eyeballing it right, I think we can set the default loaders and wrap them in the cached loader when not loaders
. Otherwise we should just wrap the set value of loaders
.
It doesn't seem to me that the cached Loader is meant to be used as a child loader and passes calls directly through to it's children bypassing the caching mechanism. We noticed a considerable increase in our CI test time once we introduced it. I might be missing something but this is what I understand currently:
This is a simple test case showing the behavior. class ChildCachedLoaderTest(TestCase):
def test_child_cached_loader(self):
wrap_loaders("django")
engine = engines["django"].engine
partial_loader = engine.template_loaders[0]
self.assertEqual(type(partial_loader).__module__, "template_partials.loader")
cached_loader = partial_loader.loaders[0]
self.assertEqual(type(cached_loader).__module__, "django.template.loaders.cached")
self.assertEqual(len(cached_loader.get_template_cache), 0)
engine.get_template("example.html")
self.assertEqual(len(cached_loader.get_template_cache), 1) # <-- Failure
@override_settings(
TEMPLATES=[{"BACKEND": "django.template.backends.django.DjangoTemplates", "APP_DIRS": True}]
)
class SanityCheckCachedTest(TestCase):
def test_no_wrap_loaders(self):
tpl_engine = django.template.engines["django"].engine
# Cached tin the top level
cached_loader = tpl_engine.template_loaders[0]
self.assertIsInstance(cached_loader, django.template.loaders.cached.Loader)
self.assertEqual(len(cached_loader.get_template_cache), 0)
tpl_engine.get_template("example.html")
self.assertEqual(len(cached_loader.get_template_cache), 1) Not entirely sure how to work around or fix the issue. The Django test cases for the loaders don't seem to go beyond one level of nesting so it doesn't feel like it's something planned for. I just skimmed them so I might have overlooked something. It does feel quite unintuitive and a missed opportunity if that's the case. |
Hi @garbelini — good spot. Yes, just eyeballing it, your analysis looks good. Do you want to move your comment wholesale to a new issue (or perhaps a PR adding the two tests)? The fix, I'd think, will be to call the cached loader's |
@carltongibson sounds good. I'll create an issue for it. Thanks for the prompt reply! |
Created #36 |
Fix #30
wrap_loaders checks if
loaders[0][0] == "template_partials.loader.Loader"
. Which it doesn't if you had set TEMPLATES["OPTIONS"]["loaders"] to anything else. django-components suggests you do this in the installation section:So
loaders[0][0]
is actually "django.template.loaders.cached.Loader". Then wrap_loaders overrides it:So the easy fix for anyone running into this is to wrap it yourself:
This does that by default by
wrap_loaders
and uses the default config as a fallback.