Code

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#15510 closed (duplicate)

Error reported when sub template is missing is wrong

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

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.

Attachments (0)

Change History (2)

comment:1 Changed 3 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #15502

comment:2 Changed 3 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.