Opened 14 years ago

Closed 14 years ago

Last modified 13 years ago

#15510 closed (duplicate)

Error reported when sub template is missing is wrong

Reported by: Lior Sion Owned by: nobody
Component: Template system Version: 1.2
Severity: Keywords:
Cc: Lior Sion Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have two templates, TemplateA and TemplateB, where TemplateB is included in TemplateA:

    {% include "sub/TemplateB.html" %}

The view used to display this template is a DetailedView (although I believe it also happend with TemplateView):

    class TempView(DetailedView):
        template_name = "subA/TemplateA.html"

if TemplateB.html is missing, the result error page displays:

TemplateDoesNotExist...
subA/TemplateA.html, subA/tempview_detail.html

and no mention to the real missing template, TemplateB

I believe it's quite like 12787, however, the fix there was wrong: it fixed {% extend %}-ed template that was missing an include, and not a template that was missing an include as reported, so I'm starting a new ticket for this.

I believe this is the technical problem + solution:

The original exception (found in loader.py, also in changeset:12792):

raise TemplateDoesNotExist(name) 

that's the correct line of code, however, at the same file (and same changeset:12792):

except TemplateDoesNotExist:
            # If compiling the template we found raises TemplateDoesNotExist, back off to
            # returning the source and display name for the template we were asked to load.
            # This allows for correct identification (later) of the actual template that does
            # not exist.
            return source, display_name

the exception is caught but value is not used, and so we "loose" the exception. However, since we try to render it again, it's thrown again at the same place. This time, it's caught, but there's an error there:

the original call look like this:

def select_template(template_name_list):
    "Given a list of template names, returns the first that can be loaded."
    for template_name in template_name_list:
        try:
            return get_template(template_name)
        except TemplateDoesNotExist:
            continue
    # If we get here, none of the templates could be loaded
    raise TemplateDoesNotExist(', '.join(template_name_list))

and so the exception caught there is not because the original template was not found, but because templateB was not found, but it's never recorded and identified.

A possible solution:

def select_template(template_name_list):
    "Given a list of template names, returns the first that can be loaded."
    error_list = []
    for template_name in template_name_list:
        try:
            return get_template(template_name)
        except TemplateDoesNotExist, e:
            if e.message not in template_name_list:
                error_list.append(e.message)
            continue
    # If we get here, none of the templates could be loaded
    template_name_list += error_list
    raise TemplateDoesNotExist(', '.join(template_name_list))

we need to make sure the reason for the TemplateDoesNotExist exception is not "just" looking for TemplateA in a wrong directory (the external loop).

A different approach would be to create SubTemplateDoesNotExist exception type.

Change History (2)

comment:1 by Russell Keith-Magee, 14 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #15502

comment:2 by Jacob, 13 years ago

milestone: 1.3

Milestone 1.3 deleted

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