diff --git a/src/backend/InvenTree/common/setting/system.py b/src/backend/InvenTree/common/setting/system.py index 00fb35a558..f026bbcd94 100644 --- a/src/backend/InvenTree/common/setting/system.py +++ b/src/backend/InvenTree/common/setting/system.py @@ -49,7 +49,8 @@ def validate_part_name_format(value): }) # Attempt to render the template with a dummy Part instance - p = Part(name='test part', description='some test part') + # Use pk=1 to ensure conditional checks like {% if part.pk %} are evaluated + p = Part(pk=1, name='test part', description='some test part') try: SandboxedEnvironment().from_string(value).render({'part': p}) diff --git a/src/backend/InvenTree/part/helpers.py b/src/backend/InvenTree/part/helpers.py index d07e3cfc13..7c55c9810e 100644 --- a/src/backend/InvenTree/part/helpers.py +++ b/src/backend/InvenTree/part/helpers.py @@ -5,7 +5,7 @@ import os from django.conf import settings import structlog -from jinja2 import Environment, select_autoescape +from jinja2.sandbox import SandboxedEnvironment from common.settings import get_global_setting @@ -37,11 +37,7 @@ def compile_full_name_template(*args, **kwargs): # Cache the template string _part_full_name_template_string = template_string - env = Environment( - autoescape=select_autoescape(default_for_string=False, default=False), - variable_start_string='{{', - variable_end_string='}}', - ) + env = SandboxedEnvironment(variable_start_string='{{', variable_end_string='}}') # Compile the template try: