Fixed #15298 -- Raise a better error when a TemplateResponseMixin doesn't have a template_name defined. Thanks to rasca for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15532 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2011-02-14 13:05:14 +00:00
parent 06b22963ea
commit bb26c328ec
5 changed files with 23 additions and 3 deletions

View File

@ -102,7 +102,9 @@ class TemplateResponseMixin(object):
a list. May not be called if render_to_response is overridden.
"""
if self.template_name is None:
return []
raise ImproperlyConfigured(
"TemplateResponseMixin requires either a definition of "
"'template_name' or an implementation of 'get_template_names()'")
else:
return [self.template_name]

View File

@ -108,7 +108,12 @@ class SingleObjectTemplateResponseMixin(TemplateResponseMixin):
Return a list of template names to be used for the request. Must return
a list. May not be called if get_template is overridden.
"""
names = super(SingleObjectTemplateResponseMixin, self).get_template_names()
try:
names = super(SingleObjectTemplateResponseMixin, self).get_template_names()
except ImproperlyConfigured:
# If template_name isn't specified, it's not a problem --
# we just start with an empty list.
names = []
# If self.template_name_field is set, grab the value of the field
# of that name from the object; this is the most specific template

View File

@ -127,7 +127,12 @@ class MultipleObjectTemplateResponseMixin(TemplateResponseMixin):
Return a list of template names to be used for the request. Must return
a list. May not be called if get_template is overridden.
"""
names = super(MultipleObjectTemplateResponseMixin, self).get_template_names()
try:
names = super(MultipleObjectTemplateResponseMixin, self).get_template_names()
except ImproperlyConfigured:
# If template_name isn't specified, it's not a problem --
# we just start with an empty list.
names = []
# If the list is a queryset, we'll invent a template name based on the
# app and model name. This name gets put at the end of the template

View File

@ -181,6 +181,12 @@ class TemplateViewTest(TestCase):
"""
self._assert_about(TemplateView.as_view(template_name='generic_views/about.html')(self.rf.get('/about/')))
def test_template_name_required(self):
"""
A template view must provide a template name
"""
self.assertRaises(ImproperlyConfigured, self.client.get, '/template/no_template/')
def test_template_params(self):
"""
A generic template view passes kwargs as context.

View File

@ -11,6 +11,8 @@ urlpatterns = patterns('',
# views.DecoratedAboutView()),
# TemplateView
(r'^template/no_template/$',
TemplateView.as_view()),
(r'^template/simple/(?P<foo>\w+)/$',
TemplateView.as_view(template_name='generic_views/about.html')),
(r'^template/custom/(?P<foo>\w+)/$',