#16147: {% include %} tag raises TemplateDoesNotExist at compile time of parent
template if TEMPLATE_DEBUG is True
-------------------------------------------------+-------------------------
 Reporter:  mrmachine                            |          Owner:  nobody
     Type:  Bug                                  |         Status:  new
Milestone:  1.4                                  |      Component:
  Version:  1.3                                  |  Template system
 Keywords:  include template                     |       Severity:  Normal
  TemplateDoesNotExist TEMPLATE_DEBUG            |   Triage Stage:
Has patch:  0                                    |  Unreviewed
                                                 |  Easy pickings:  0
-------------------------------------------------+-------------------------
 I think this is a bug. Or at the very least, it can make for unexpected
 behaviour that is difficult to trace. When developing locally, `DEBUG` and
 `TEMPLATE_DEBUG` are often `True`. When `TEMPLATE_DEBUG` is `True`,
 compiling a template that contains an `{% include %}` to a template that
 doesn't exist will raise `TemplateDoesNotExist` at compile time of the
 parent template.

 {{{
 >>> from django.template import Template
 >>> t = Template('{% include "missing.html" %}')
 Traceback (most recent call last):
 ...
 TemplateDoesNotExist: missing.html
 }}}

 The problem is that when you try to conditionally load a template, and
 perform a different action if it does not exist. For example, you might
 have a view that uses `select_template` to load one of several templates
 (if one exists) and send an email. If no email was sent (because no
 template exists), render a different template/context to the response or
 take some other action. If the email template contains a typo in the name
 of an included template, the email template will not be loaded and no
 email will be sent because `TemplateDoesNotExist` was raised.

 In the local dev environment, this will not display a pretty debug page
 showing the error inside the email template, it will simply behave
 unexpectedly (by not sending an email when the email template DOES exist).
 On the production server (or when `TEMPLATE_DEBUG` is `False`), it will
 behave as expected. The email template will be found and it will be
 rendered.

 This problem doesn't exist when using a variable as the template name.

 {{{
 >>> t = Template('{% include somevar %}')
 >>>
 }}}

 Why does Django try to execute the `{% include %}` when it contains a hard
 coded string at compile time at all? I think this is a case of premature
 optimisation. If it execution of the `{% include %}` was delayed until
 render time, `select_template` would correctly return the email template
 when `TEMPLATE_DEBUG` is `True`, and the proper error would be displayed
 in the rendered email template (making it an easy fix to correct the
 typo).

-- 
Ticket URL: <https://code.djangoproject.com/ticket/16147>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to