Opened 6 years ago
Closed 5 years ago
#30425 closed Bug (fixed)
Handle `jinja2.TemplateSyntaxError` when rendering a template with or without a source.
Reported by: | Daniel Hahler | Owned by: | Hasan Ramezani |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The "source" attribute might be None with syntax error exceptions with Jinja2 templates.
The current code (https://github.com/django/django/blob/673fe2e3ec63614259e86e7a370b9d1e91fcc1e1/django/template/backends/jinja2.py#L84-L108) does not handle it, but could read the source then.
See https://github.com/niwinz/django-jinja/pull/233 for a fix with more details.
Change History (4)
comment:1 by , 6 years ago
Summary: | Jinja: get_exception_info: source might be None → Handle `jinja2.TemplateSyntaxError` when rendering a template with or without a source. |
---|---|
Triage Stage: | Unreviewed → Accepted |
Version: | 2.2 → master |
comment:2 by , 6 years ago
Indeed I forgot to mention that I've also wrapped render
in the first place.
Just for reference, the patch for render
for django-jinja:
diff --git i/django_jinja/backend.py w/django_jinja/backend.py index 7663186..ffc64c7 100644 --- i/django_jinja/backend.py +++ w/django_jinja/backend.py @@ -103,7 +105,12 @@ def dicts(self): template=self, context=context) - return mark_safe(self.template.render(context)) + try: + return mark_safe(self.template.render(context)) + except jinja2.TemplateSyntaxError as exc: + new = TemplateSyntaxError(exc.args) + new.template_debug = get_exception_info(exc) + six.reraise(TemplateSyntaxError, new, sys.exc_info()[2]) class Jinja2(BaseEngine):
comment:3 by , 5 years ago
Has patch: | set |
---|---|
Owner: | changed from | to
Status: | new → assigned |
We don't catch
jinja2.TemplateSyntaxError
in `Template.render()`, henceget_exception_info()
is not called whenjinja2.TemplateSyntaxError
is raised in rendering i.e. whensource
can beNone
.I agree that we should catch
jinja2.TemplateSyntaxError
inTemplate.render()
and in such case fix issue withsource = None
: