From 7d02fa94332b43c7527c1b816787b4c560cf6bf6 Mon Sep 17 00:00:00 2001 From: Keryn Knight Date: Tue, 30 Nov 2021 13:07:45 +0000 Subject: [PATCH] Refs #32290 -- Optimized construct_relative_path() by delay computing has_quotes. --- django/template/loader_tags.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/django/template/loader_tags.py b/django/template/loader_tags.py index e0ab7b6bddb..3e770751d1f 100644 --- a/django/template/loader_tags.py +++ b/django/template/loader_tags.py @@ -235,10 +235,6 @@ 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. """ - 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 @@ -262,6 +258,10 @@ def construct_relative_path(current_template_name, relative_name): "same template in which the tag appears." % (relative_name, current_template_name) ) + has_quotes = ( + relative_name.startswith(('"', "'")) and + relative_name[0] == relative_name[-1] + ) return f'"{new_name}"' if has_quotes else new_name