Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#21058 closed Bug (fixed)

Template Error during render results in 500 error

Reported by: jambonrose Owned by: ianawilson
Component: Template system Version: 1.5
Severity: Normal Keywords: template, TemplateDoesNotExist, render
Cc: ianawilson Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Consider:

from django.shortcuts import render

def testview(request):
    return render(request, [], {})

Accessing this view will raise a 500 error:
A server error occurred. Please contact the administrator.

The traceback is as follows:

Internal Server Error: /path_to_view/
Traceback (most recent call last):
  File "/Users/jambonrose/.virtualenvs/djtest/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/Users/jambonrose/djtest/cbgvtest/views.py", line 4, in testview
    return render(request, [], {})
  File "/Users/jambonrose/.virtualenvs/djtest/lib/python2.7/site-packages/django/shortcuts/__init__.py", line 53, in render
    return HttpResponse(loader.render_to_string(*args, **kwargs),
  File "/Users/jambonrose/.virtualenvs/djtest/lib/python2.7/site-packages/django/template/loader.py", line 168, in render_to_string
    t = select_template(template_name)
  File "/Users/jambonrose/.virtualenvs/djtest/lib/python2.7/site-packages/django/template/loader.py", line 184, in select_template
    raise TemplateDoesNotExist("No template names provided")
TemplateDoesNotExist: No template names provided
Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/Users/jambonrose/.virtualenvs/djtest/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 72, in __call__
    return self.application(environ, start_response)
  File "/Users/jambonrose/.virtualenvs/djtest/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 255, in __call__
    response = self.get_response(request)
  File "/Users/jambonrose/.virtualenvs/djtest/lib/python2.7/site-packages/django/core/handlers/base.py", line 178, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/Users/jambonrose/.virtualenvs/djtest/lib/python2.7/site-packages/django/core/handlers/base.py", line 217, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/Users/jambonrose/.virtualenvs/djtest/lib/python2.7/site-packages/django/views/debug.py", line 69, in technical_500_response
    html = reporter.get_traceback_html()
  File "/Users/jambonrose/.virtualenvs/djtest/lib/python2.7/site-packages/django/views/debug.py", line 297, in get_traceback_html
    c = Context(self.get_traceback_data())
  File "/Users/jambonrose/.virtualenvs/djtest/lib/python2.7/site-packages/django/views/debug.py", line 237, in get_traceback_data
    for loader in template_source_loaders:
TypeError: 'NoneType' object is not iterable
[06/Sep/2013 15:19:35] "GET /try/ HTTP/1.1" 500 59

Django should instead be raising a TemplateDoesNotExist error.

The problem stems from interaction between get_traceback_data in django/views/debug.py and find_template in django/template/loader.py - the former assumes the latter has already run and provided template_source_loaders with values in a list (an empty list is valid behavior). However, in the case above, find_template is not run, and template_source_loaders is set to None.

The included patch checks for this, and replaces None with [] resulting with the correct behavior.

Attachments (1)

django_get_traceback_data.patch (583 bytes ) - added by jambonrose 11 years ago.
Patch of get_traceback_data method.

Download all attachments as: .zip

Change History (6)

by jambonrose, 11 years ago

Patch of get_traceback_data method.

comment:1 by ianawilson, 11 years ago

Pull request for this here: https://github.com/django/django/pull/1579

comment:2 by ianawilson, 11 years ago

Owner: changed from jambonrose to ianawilson
Status: newassigned

comment:3 by Ian Wilson <ian.owings@…>, 11 years ago

Resolution: fixed
Status: assignedclosed

In 9b7f4aab326067a3ad503887a4e42c9d76d0bc25:

adds fix and test for when a template is not specified at all to render(). fixes #21058. by jambonrose and ianawilson

comment:4 by Russell Keith-Magee <russell@…>, 11 years ago

In 122020fdb93980df850ae02f61d97da27e2cb515:

Merge pull request #1579 from ianawilson/ticket_21058

Fixed #21058 -- Fixed debug view blowing up when no template is provided to the template rendering functions.

Assistance on this commit from @jambonrose.

comment:5 by Russell Keith-Magee <russell@…>, 11 years ago

In b917458f4752e98dc7866261f40e296de3701130:

Merge pull request #1579 from ianawilson/ticket_21058

[1.6.x] Fixed #21058 -- Fixed debug view blowing up when no template is provided to the template rendering functions.

Assistance on this commit from @jambonrose.

Backport of 122020fdb93980df850ae02f61d97da27e2cb515 from master.

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