Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

adds fix for SingleObjectTemplateResponseMixin raising a TemplateDoes…

…NotExist when it should have raised an ImproperlyConfigured. fixes 16502. by @ianawilson, @jambonrose
  • Loading branch information...
commit b79df0b35861400f573e74d0305d7298c221e7ff 1 parent 630eb05
Ian A Wilson authored
50  django/views/generic/detail.py
@@ -137,28 +137,34 @@ def get_template_names(self):
137 137
             # we just start with an empty list.
138 138
             names = []
139 139
 
140  
-        # If self.template_name_field is set, grab the value of the field
141  
-        # of that name from the object; this is the most specific template
142  
-        # name, if given.
143  
-        if self.object and self.template_name_field:
144  
-            name = getattr(self.object, self.template_name_field, None)
145  
-            if name:
146  
-                names.insert(0, name)
147  
-
148  
-        # The least-specific option is the default <app>/<model>_detail.html;
149  
-        # only use this if the object in question is a model.
150  
-        if isinstance(self.object, models.Model):
151  
-            names.append("%s/%s%s.html" % (
152  
-                self.object._meta.app_label,
153  
-                self.object._meta.model_name,
154  
-                self.template_name_suffix
155  
-            ))
156  
-        elif hasattr(self, 'model') and self.model is not None and issubclass(self.model, models.Model):
157  
-            names.append("%s/%s%s.html" % (
158  
-                self.model._meta.app_label,
159  
-                self.model._meta.model_name,
160  
-                self.template_name_suffix
161  
-            ))
  140
+            # If self.template_name_field is set, grab the value of the field
  141
+            # of that name from the object; this is the most specific template
  142
+            # name, if given.
  143
+            if self.object and self.template_name_field:
  144
+                name = getattr(self.object, self.template_name_field, None)
  145
+                if name:
  146
+                    names.insert(0, name)
  147
+
  148
+            # The least-specific option is the default <app>/<model>_detail.html;
  149
+            # only use this if the object in question is a model.
  150
+            if isinstance(self.object, models.Model):
  151
+                names.append("%s/%s%s.html" % (
  152
+                    self.object._meta.app_label,
  153
+                    self.object._meta.model_name,
  154
+                    self.template_name_suffix
  155
+                ))
  156
+            elif hasattr(self, 'model') and self.model is not None and issubclass(self.model, models.Model):
  157
+                names.append("%s/%s%s.html" % (
  158
+                    self.model._meta.app_label,
  159
+                    self.model._meta.model_name,
  160
+                    self.template_name_suffix
  161
+                ))
  162
+
  163
+            # If we still haven't managed to find any template names, we should
  164
+            # re-raise the ImproperlyConfigured to alert the user.
  165
+            if not names:
  166
+                raise
  167
+
162 168
         return names
163 169
 
164 170
 
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_create_view_with_form_only(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 b79df0b

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