Open copyNdpaste opened 5 months ago
SERVER_ENV=local ./manage.py spectacular --file schema.yaml --validate -> Schema generation summary: Warnings: 1 (1 unique) Errors: 0 (0 unique)
Hi, this is highly unusual. I'm not even sure how it is possible that line 22 produces an error. By definition the if
is safe even if the variable is not existing, in which case false
is assumed.
@copyNdpaste do you have Django Debug Toolbar installed? I run into these errors when the Toolbar is visible.
This is just a debug level log from the 'django' logger, it logs the exception but replaces the variable with context.template.engine.string_if_invalid, which is an empty string by default, so there are no errors in request or templates rendering. You can check the code in the file "/usr/local/lib/python3.9/site-packages/django/template/base.py" or https://github.com/django/django/blob/fa7848146738a9fe1d415ee4808664e54739eeb7/django/template/base.py#L931-L945 for example.
...
except Exception as e:
template_name = getattr(context, "template_name", None) or "unknown"
logger.debug(
"Exception while resolving variable '%s' in template '%s'.",
bit,
template_name,
exc_info=True,
)
if getattr(e, "silent_variable_failure", False):
current = context.template.engine.string_if_invalid
else:
raise
return current
https://docs.djangoproject.com/en/5.0/ref/templates/api/#how-invalid-variables-are-handled
thx @vcleal this finally makes sense, I believe. Since there is no nice&short way of checking for existence, it is easier to just set this var to None
in the main use-case.
I think this should fix it then. Feedback welcome.
settings.py
urls.py
why VariableDoesNotExist error is raised?