Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #15298 -- Raise a better error when a TemplateResponseMixin doe…

…sn'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
  • Loading branch information...
commit bb26c328ecb831889bedb0c8354a93c8176187c6 1 parent 06b2296
@freakboy3742 freakboy3742 authored
View
4 django/views/generic/base.py
@@ -102,7 +102,9 @@ def get_template_names(self):
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
7 django/views/generic/detail.py
@@ -108,7 +108,12 @@ def get_template_names(self):
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
7 django/views/generic/list.py
@@ -127,7 +127,12 @@ def get_template_names(self):
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
6 tests/regressiontests/generic_views/base.py
@@ -181,6 +181,12 @@ def test_get_generic_template(self):
"""
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
2  tests/regressiontests/generic_views/urls.py
@@ -11,6 +11,8 @@
# 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+)/$',
Please sign in to comment.
Something went wrong with that request. Please try again.