summaryrefslogtreecommitdiff
path: root/django/template
diff options
context:
space:
mode:
authorHasan Ramezani <hasan.r67@gmail.com>2020-12-27 21:21:59 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-01-27 10:07:31 +0100
commitc978dd93fda87c6d2e965d385164c35f1a3e64b8 (patch)
treef76195ed3d24a889175ba5a5ad730643991c087d /django/template
parent640a6e1dce8d91e9cb1f817fadbb37a2c9d294bf (diff)
Fixed #32290 -- Fixed TemplateNotFound in {% include %} tag for relative path in variable.
Diffstat (limited to 'django/template')
-rw-r--r--django/template/loader_tags.py16
1 files changed, 12 insertions, 4 deletions
diff --git a/django/template/loader_tags.py b/django/template/loader_tags.py
index b96214cad1..07b614c270 100644
--- a/django/template/loader_tags.py
+++ b/django/template/loader_tags.py
@@ -171,7 +171,10 @@ class IncludeNode(Node):
# If not, try the cache and select_template().
template_name = template or ()
if isinstance(template_name, str):
- template_name = (template_name,)
+ template_name = (construct_relative_path(
+ self.origin.template_name,
+ template_name,
+ ),)
else:
template_name = tuple(template_name)
cache = context.render_context.dicts[0].setdefault(self, {})
@@ -226,7 +229,12 @@ def construct_relative_path(current_template_name, relative_name):
Convert a relative path (starting with './' or '../') to the full template
name based on the current_template_name.
"""
- if not relative_name.startswith(("'./", "'../", '"./', '"../')):
+ has_quotes = (
+ (relative_name.startswith('"') and relative_name.endswith('"')) or
+ (relative_name.startswith("'") and relative_name.endswith("'"))
+ )
+ new_name = relative_name.strip('\'"')
+ if not new_name.startswith(('./', '../')):
# relative_name is a variable or a literal that doesn't contain a
# relative path.
return relative_name
@@ -234,7 +242,7 @@ def construct_relative_path(current_template_name, relative_name):
new_name = posixpath.normpath(
posixpath.join(
posixpath.dirname(current_template_name.lstrip('/')),
- relative_name.strip('\'"')
+ new_name,
)
)
if new_name.startswith('../'):
@@ -248,7 +256,7 @@ def construct_relative_path(current_template_name, relative_name):
"same template in which the tag appears."
% (relative_name, current_template_name)
)
- return '"%s"' % new_name
+ return f'"{new_name}"' if has_quotes else new_name
@register.tag('extends')