1
0
mirror of https://github.com/django/django.git synced 2025-04-08 15:36:47 +00:00

Fixed #36271 -- Raised TemplateSyntaxError when using a relative template path with an unknown origin.

This commit is contained in:
YogyaChugh 2025-03-22 18:13:26 +05:30 committed by Sarah Boyce
parent 0b4f2d8d39
commit 7164f08047
2 changed files with 19 additions and 1 deletions

View File

@ -257,6 +257,13 @@ def construct_relative_path(
# relative path.
return relative_name
if current_template_name is None:
# Unknown origin (e.g. Template('...').render(Context({...})).
raise TemplateSyntaxError(
f"The relative path {relative_name} cannot be evaluated due to "
"an unknown template origin."
)
new_name = posixpath.normpath(
posixpath.join(
posixpath.dirname(current_template_name.lstrip("/")),

View File

@ -1,4 +1,4 @@
from django.template import TemplateDoesNotExist, TemplateSyntaxError
from django.template import Template, TemplateDoesNotExist, TemplateSyntaxError
from django.test import SimpleTestCase
from ..utils import setup
@ -64,3 +64,14 @@ class ExceptionsTests(SimpleTestCase):
"""
with self.assertRaises(TemplateSyntaxError):
self.engine.render_to_string("exception05")
def test_unknown_origin_relative_path(self):
files = ["./nonexistent.html", "../nonexistent.html"]
for template_name in files:
with self.subTest(template_name=template_name):
msg = (
f"The relative path '{template_name}' cannot be evaluated due to "
"an unknown template origin."
)
with self.assertRaisesMessage(TemplateSyntaxError, msg):
Template(f"{{% extends '{template_name}' %}}")