Bug : Relative path in extends and include does not work when using template_file (#976)

Co-authored-by: Juro Oravec <juraj.oravec.josefson@gmail.com>
This commit is contained in:
lilian D 2025-02-16 13:29:37 +01:00 committed by GitHub
parent 3cf586a974
commit 58d4c78671
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 64 additions and 1 deletions

View file

@ -737,7 +737,10 @@ class Component(
template: Template = cached_template(
template_string=template_body,
name=self.template_file or self.name,
origin=Origin(name=self.template_file or get_import_path(self.__class__)),
origin=Origin(
name=self.template_file or get_import_path(self.__class__),
template_name=self.template_file or self.name,
),
)
else:
template = template_body

View file

@ -0,0 +1,5 @@
{% extends './block.html' %}
{% block body %}
BLOCK OVERRIDEN
{% endblock %}

View file

@ -18,6 +18,15 @@ class BlockedAndSlottedComponent(Component):
template_file = "blocked_and_slotted_template.html"
class RelativeFileComponentUsingTemplateFile(Component):
template_file = "relative_extends.html"
class RelativeFileComponentUsingGetTemplateName(Component):
def get_template_name(self, context):
return "relative_extends.html"
#######################
# TESTS
#######################
@ -836,3 +845,49 @@ class ExtendsCompatTests(BaseTestCase):
</html>
"""
self.assertHTMLEqual(rendered, expected)
@parametrize_context_behavior(["django", "isolated"])
def test_component_using_template_file_extends_relative_file(self):
registry.register("relative_file_component_using_template_file", RelativeFileComponentUsingTemplateFile)
template: types.django_html = """
{% load component_tags %}
{% component "relative_file_component_using_template_file" %}{% endcomponent %}
"""
rendered = Template(template).render(Context())
expected = """
<!DOCTYPE html>
<html data-djc-id-a1bc3f="" lang="en">
<body>
<main role="main">
<div class='container main-container'>
BLOCK OVERRIDEN
</div>
</main>
</body>
</html>
"""
self.assertHTMLEqual(rendered, expected)
@parametrize_context_behavior(["django", "isolated"])
def test_component_using_get_template_name_extends_relative_file(self):
registry.register("relative_file_component_using_get_template_name", RelativeFileComponentUsingGetTemplateName)
template: types.django_html = """
{% load component_tags %}
{% component "relative_file_component_using_get_template_name" %}{% endcomponent %}
"""
rendered = Template(template).render(Context())
expected = """
<!DOCTYPE html>
<html data-djc-id-a1bc3f="" lang="en">
<body>
<main role="main">
<div class='container main-container'>
BLOCK OVERRIDEN
</div>
</main>
</body>
</html>
"""
self.assertHTMLEqual(rendered, expected)