Update template loaders for new API.
Review Request #11963 — Created Jan. 21, 2022 and submitted
Django 1.9 changed the template loader API, and 2.0 removed the old
methods. This change updates our extension and namespaced app dir
loaders to use the new API instead.
Ran unit tests.
Summary | ID |
---|---|
47733504234add359ee21ce300fe79c9a09c0fbc |
Description | From | Last Updated |
---|---|---|
Thought I had posted this for review, but it turns out it's still in my tree. Anyway, I also tackled … |
chipx86 |
-
-
Thought I had posted this for review, but it turns out it's still in my tree. Anyway, I also tackled this.
We need to do one more thing, I think. Stuffing the contents inside
name
isn't correct. Instead, what we need to do is subclassOrigin
and provide the right metadata for discovery. We then want to overrideget_contents()
to do theresource_string()
based on that metadata.Here's what I had:
class ExtensionOrigin(Origin): """An origin for a template in an extension. Version Added: 3.0 """ def __init__(self, package, resource, *args, **kwargs): """Initialize the origin. Args: package (unicode): The name of the package providing the template. resource (unicode): The resource path within the package. *args (tuple): Positional arguments to pass to the parent. **kwargs (dict): Keyword arguments to pass to the parent. """ self.package = package self.resource = resource super(ExtensionOrigin, self).__init__(*args, **kwargs) class Loader(BaseLoader): ... def get_contents(self, origin): """Return the contents of a template. Args: origin (ExtensionOrigin): The origin of the template. Returns: unicode: The resulting template contents. """ try: return resource_string(origin.package, origin.resource) except Exception: raise TemplateDoesNotExist(origin) def get_template_sources(self, template_name): """Load templates from enabled extensions. Args: template_name (unicode): The name of the template to load. Yields: ExtensionOrigin: Each possible location for the template. """ if manager: resource = 'templates/%s' % template_name for extension_mgr in get_extension_managers(): for extension in extension_mgr.get_enabled_extensions(): package = ext.info.app_name yield ExtensionOrigin( package=package, resource=resource, name='extension:%s:%s' % (package, resource), template_name=template_name, loader=self)