Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.6.x] Merge pull request #1580 from ianawilson/ticket_16502

Fixed #16502 -- Fixed a TemplateDoesNotExist error that should be an ImproperlyConfigured.

Assistance on the patch from #jambronrose.

Backport of 9b2dc12 from master.
  • Loading branch information...
commit 99952bab3008622b05613ed6ec54c3e1c63c0a1d 1 parent b917458
Russell Keith-Magee authored September 06, 2013
50  django/views/generic/detail.py
@@ -133,28 +133,34 @@ def get_template_names(self):
133 133
             # we just start with an empty list.
134 134
             names = []
135 135
 
136  
-        # If self.template_name_field is set, grab the value of the field
137  
-        # of that name from the object; this is the most specific template
138  
-        # name, if given.
139  
-        if self.object and self.template_name_field:
140  
-            name = getattr(self.object, self.template_name_field, None)
141  
-            if name:
142  
-                names.insert(0, name)
143  
-
144  
-        # The least-specific option is the default <app>/<model>_detail.html;
145  
-        # only use this if the object in question is a model.
146  
-        if isinstance(self.object, models.Model):
147  
-            names.append("%s/%s%s.html" % (
148  
-                self.object._meta.app_label,
149  
-                self.object._meta.model_name,
150  
-                self.template_name_suffix
151  
-            ))
152  
-        elif hasattr(self, 'model') and self.model is not None and issubclass(self.model, models.Model):
153  
-            names.append("%s/%s%s.html" % (
154  
-                self.model._meta.app_label,
155  
-                self.model._meta.model_name,
156  
-                self.template_name_suffix
157  
-            ))
  136
+            # If self.template_name_field is set, grab the value of the field
  137
+            # of that name from the object; this is the most specific template
  138
+            # name, if given.
  139
+            if self.object and self.template_name_field:
  140
+                name = getattr(self.object, self.template_name_field, None)
  141
+                if name:
  142
+                    names.insert(0, name)
  143
+
  144
+            # The least-specific option is the default <app>/<model>_detail.html;
  145
+            # only use this if the object in question is a model.
  146
+            if isinstance(self.object, models.Model):
  147
+                names.append("%s/%s%s.html" % (
  148
+                    self.object._meta.app_label,
  149
+                    self.object._meta.model_name,
  150
+                    self.template_name_suffix
  151
+                ))
  152
+            elif hasattr(self, 'model') and self.model is not None and issubclass(self.model, models.Model):
  153
+                names.append("%s/%s%s.html" % (
  154
+                    self.model._meta.app_label,
  155
+                    self.model._meta.model_name,
  156
+                    self.template_name_suffix
  157
+                ))
  158
+
  159
+            # If we still haven't managed to find any template names, we should
  160
+            # re-raise the ImproperlyConfigured to alert the user.
  161
+            if not names:
  162
+                raise
  163
+
158 164
         return names
159 165
 
160 166
 
12  tests/generic_views/test_base.py
@@ -468,3 +468,15 @@ def test_overwrite_queryset(self):
468 468
         # Overwrite the view's queryset with queryset from kwarg
469 469
         context = test_view.get_context_data(object_list=queryset)
470 470
         self.assertEqual(context['object_list'], queryset)
  471
+
  472
+
  473
+class SingleObjectTemplateResponseMixinTest(unittest.TestCase):
  474
+
  475
+    def test_template_mixin_without_template(self):
  476
+        """
  477
+        We want to makes sure that if you use a template mixin, but forget the
  478
+        template, it still tells you it's ImproperlyConfigured instead of
  479
+        TemplateDoesNotExist.
  480
+        """
  481
+        view = views.TemplateResponseWithoutTemplate()
  482
+        self.assertRaises(ImproperlyConfigured, view.get_template_names)
8  tests/generic_views/views.py
@@ -92,6 +92,14 @@ class NaiveAuthorCreate(generic.CreateView):
92 92
     fields = '__all__'
93 93
 
94 94
 
  95
+class TemplateResponseWithoutTemplate(generic.detail.SingleObjectTemplateResponseMixin, generic.View):
  96
+    # we don't define the usual template_name here
  97
+
  98
+    def __init__(self):
  99
+        # Dummy object, but attr is required by get_template_name()
  100
+        self.object = None
  101
+
  102
+
95 103
 class AuthorCreate(generic.CreateView):
96 104
     model = Author
97 105
     success_url = '/list/authors/'

0 notes on commit 99952ba

Please sign in to comment.
Something went wrong with that request. Please try again.