Skip to content

Commit 7164f08

Browse files
YogyaChughsarahboyce
authored andcommitted
Fixed #36271 -- Raised TemplateSyntaxError when using a relative template path with an unknown origin.
1 parent 0b4f2d8 commit 7164f08

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Diff for: django/template/loader_tags.py

+7
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ def construct_relative_path(
257257
# relative path.
258258
return relative_name
259259

260+
if current_template_name is None:
261+
# Unknown origin (e.g. Template('...').render(Context({...})).
262+
raise TemplateSyntaxError(
263+
f"The relative path {relative_name} cannot be evaluated due to "
264+
"an unknown template origin."
265+
)
266+
260267
new_name = posixpath.normpath(
261268
posixpath.join(
262269
posixpath.dirname(current_template_name.lstrip("/")),

Diff for: tests/template_tests/syntax_tests/test_exceptions.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from django.template import TemplateDoesNotExist, TemplateSyntaxError
1+
from django.template import Template, TemplateDoesNotExist, TemplateSyntaxError
22
from django.test import SimpleTestCase
33

44
from ..utils import setup
@@ -64,3 +64,14 @@ def test_exception05(self):
6464
"""
6565
with self.assertRaises(TemplateSyntaxError):
6666
self.engine.render_to_string("exception05")
67+
68+
def test_unknown_origin_relative_path(self):
69+
files = ["./nonexistent.html", "../nonexistent.html"]
70+
for template_name in files:
71+
with self.subTest(template_name=template_name):
72+
msg = (
73+
f"The relative path '{template_name}' cannot be evaluated due to "
74+
"an unknown template origin."
75+
)
76+
with self.assertRaisesMessage(TemplateSyntaxError, msg):
77+
Template(f"{{% extends '{template_name}' %}}")

0 commit comments

Comments
 (0)