#12787: TemplateDoesNotExist exception does not report the correct template_name
--------------------------------------+-------------------------------------
          Reporter:  trigeek38        |         Owner:  anonymous           
            Status:  assigned         |     Milestone:  1.2                 
         Component:  Template system  |       Version:  SVN                 
        Resolution:                   |      Keywords:  TemplateDoesNotExist
             Stage:  Accepted         |     Has_patch:  1                   
        Needs_docs:  0                |   Needs_tests:  1                   
Needs_better_patch:  0                |  
--------------------------------------+-------------------------------------
Changes (by kmtracey):

  * needs_docs:  1 => 0
  * needs_tests:  0 => 1

Comment:

 jkatzer: In the future please attach a single diff file created by svn
 diff from the root of the tree. See:
 http://docs.djangoproject.com/en/dev/internals/contributing/#patch-style.
 That's much easier to deal with than a bunch of individual .diff files
 zipped up.

 I'm uncomfortable with the originally proposed way of fixing this, so I've
 attached an alternative approach. The root cause of this bug is similar to
 #12992. During `load_template`, the load of the requested template may
 succeed but then `get_template_from_string` may raise
 !TemplateDoesNotExist if the loaded template can't be compiled due to some
 other template not existing. That !TemplateDoesNotExist, raised by
 `load_template`, is interpreted by its caller to mean that the requested
 template doesn't exist, leading to an erroneous report of the actual
 problem on the debug page.

 Attached patch changes the loader to code to fall back to returning the
 template source and name, as it used to, if it was able to find the
 requested template but attempting to compile it raises a
 !TemplateSyntaxError. Thus the loader will only raise !TemplateSyntaxError
 if the specifically-requested template does not exist. That prevents this
 code:

 {{{
 #!python
     for loader in template_source_loaders:
         try:
             source, display_name = loader(name, dirs)
             return (source, make_origin(display_name, loader, name, dirs))
         except TemplateDoesNotExist:
             pass
     raise TemplateDoesNotExist(name)
 }}}

 from moving on and trying other loaders, then ultimately ending with
 `TemplateDoesNotExist(name)`, which mis-identifies the template that does
 not exist.

 That change alone is not sufficient to fix the problem, though. The
 extends node `get_parent` method catches !TemplateDoesNotExist and turns
 it into a !TemplateSyntaxError stating that the template specified to be
 extended does not exist. Prior to r11862, the call covered by the
 try/except was a `find_template_source` call, which would only raise
 !TemplateDoesNotExist if that specific template does not exist. In r11862
 the call inside the try/except was changed to `get_template`, which both
 finds the source and compiles it, and so may raise !TemplateDoesNotExist
 if some other template needed to compile the extended template does not
 exist. We could either put the code here back the way it was before r11862
 or remove this try/except entirely. The attached patch does the latter,
 because I don't really see what additional value is added by the different
 message ("... can't be extended because it doesn't exist") over a plain
 template does not exist message.

 There is a test in the patch, but it's not really sufficient since it does
 not test both the base loader case and the cached loader case. But it does
 illustrate when the problem arises.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/12787#comment:6>
Django <http://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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to