Ticket #18853: django-bug-template-names-with-queryset.patch

File django-bug-template-names-with-queryset.patch, 2.7 KB (added by Melvyn Sopacua, 12 years ago)

Fix and testcase

  • django/views/generic/detail.py

    diff -r 78e735c39934 django/views/generic/detail.py
    a b  
    139139            if name:
    140140                names.insert(0, name)
    141141
     142        def format_name(meta) :
     143            return "%s/%s%s.html" % (
     144                    meta.app_label,
     145                    meta.object_name.lower(),
     146                    self.template_name_suffix,
     147                )
    142148        # The least-specific option is the default <app>/<model>_detail.html;
    143149        # only use this if the object in question is a model.
    144150        if isinstance(self.object, models.Model):
    145             names.append("%s/%s%s.html" % (
    146                 self.object._meta.app_label,
    147                 self.object._meta.object_name.lower(),
    148                 self.template_name_suffix
    149             ))
     151            names.append(format_name(self.object._meta))
    150152        elif hasattr(self, 'model') and self.model is not None and issubclass(self.model, models.Model):
    151             names.append("%s/%s%s.html" % (
    152                 self.model._meta.app_label,
    153                 self.model._meta.object_name.lower(),
    154                 self.template_name_suffix
    155             ))
     153            names.append(format_name(self.model._meta))
     154        elif hasattr(self, 'queryset') and self.queryset is not None and hasattr(self.queryset, 'model') :
     155            names.append(format_name(self.queryset.model._meta))
     156        # Raise the exception early on because this method is not part of the
     157        # traceback generated by the resulting exception in template/loader.py
     158        # select_template method. If another way is supported in the future to
     159        # specify the object we're operating on, the logic to obtain it's meta
     160        # class should be added above this comment.
     161        elif not names :
     162            raise ImproperlyConfigured("No template name set and unable to generate candidates.")
    156163        return names
    157164
    158165
  • tests/regressiontests/generic_views/edit.py

    diff -r 78e735c39934 tests/regressiontests/generic_views/edit.py
    a b  
    8989        self.assertRedirects(res, reverse('author_detail', kwargs={'pk': obj.pk}))
    9090        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
    9191
     92    def test_create_form_using_queryset(self) :
     93        res = self.client.get('/edit/authors/create/naive/')
     94        self.assertEqual(res.status_code, 200)
     95        self.assertTemplateUsed('generic_views/author_form.html')
     96
    9297    def test_create_without_redirect(self):
    9398        try:
    9499            res = self.client.post('/edit/authors/create/naive/',
Back to Top