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 Mariusz Felisiak, 6 years ago

Summary: Jinja: get_exception_info: source might be NoneHandle `jinja2.TemplateSyntaxError` when rendering a template with or without a source.
Triage Stage: UnreviewedAccepted
Version: 2.2master

We don't catch jinja2.TemplateSyntaxError in `Template.render()`, hence get_exception_info() is not called when jinja2.TemplateSyntaxError is raised in rendering i.e. when source can be None.

I agree that we should catch jinja2.TemplateSyntaxError in Template.render() and in such case fix issue with source = None:

def render(self, context=None, request=None):
    try:    
        ...
        return self.template.render(context)
    except jinja2.TemplateSyntaxError as exc:
        new = TemplateSyntaxError(exc.args)
        new.template_debug = get_exception_info(exc)
        raise new from exc

comment:2 by Daniel Hahler, 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):
Last edited 6 years ago by Daniel Hahler (previous) (diff)

comment:3 by Hasan Ramezani, 5 years ago

Has patch: set
Owner: changed from nobody to Hasan Ramezani
Status: newassigned

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 8d322902:

Fixed #30425 -- Handled jinja2.TemplateSyntaxError when rendering a template.

Jinja raises jinja2.TemplateSyntaxError in render() not in
get_template() when it's in an included template.

Note: See TracTickets for help on using tickets.
Back to Top