Opened 3 years ago

Closed 2 years ago

#18853 closed Bug (duplicate)

get_template_names() does not consider the queryset-only case

Reported by: msopacua Owned by: apollo13
Component: Generic views Version: master
Severity: Normal Keywords: CBV
Cc: jason@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: yes UI/UX: no

Description

In SingleObjectTemplateResponseMixin candidate names are generated using the model name. This model name is derived either from self.object or self.model but does not consider self.queryset. In the case of "create forms", self.object is unset and if self.model is not provided either, an exception is thrown much later by the template loader in select_template. This exception does not point to this method as it successfully returns an (empty) list.
The test suite kind of contains a test for this (generic_views/edit.py:test_create_without_redirect), but the problem is that the test posts data to the form and does not actually request the form. In the practical case this form could never have been submitted as it wouldn't be rendered.
Patch attached fixes the issue and creates a test.

Attachments (1)

django-bug-template-names-with-queryset.patch (2.7 KB) - added by msopacua 3 years ago.
Fix and testcase

Download all attachments as: .zip

Change History (10)

Changed 3 years ago by msopacua

Fix and testcase

comment:1 follow-up: Changed 3 years ago by charettes

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

IMHO replacing the hasattr(self, 'queryset') and self.queryset is not None part by getattr(self, 'queryset', None) is not None would increase readability.

comment:2 in reply to: ↑ 1 Changed 3 years ago by msopacua

Replying to charettes:

IMHO replacing the hasattr(self, 'queryset') and self.queryset is not None part by getattr(self, 'queryset', None) is not None would increase readability.

Agreed, but it's inline with the coding style of the file, see line 150 right above. Style fixes are the committers' realm. ;)

comment:3 Changed 3 years ago by jwmayfield

  • Cc jason@… added
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Uncategorized to Bug

Patch applies cleanly. Test fails before applying fix. Test passes after applying fix.

comment:4 Changed 3 years ago by jwmayfield

  • Owner changed from nobody to jwmayfield
  • Status changed from new to assigned
  • Triage Stage changed from Accepted to Ready for checkin

comment:5 Changed 3 years ago by jwmayfield

  • Owner changed from jwmayfield to nobody
  • Status changed from assigned to new

comment:6 Changed 3 years ago by bhuztez

#16502 is a similar problem. I guess patch there may also fix this problem.

comment:7 Changed 2 years ago by apollo13

  • Owner changed from nobody to apollo13
  • Status changed from new to assigned

comment:8 Changed 2 years ago by timo

  • Patch needs improvement set
  • Triage Stage changed from Ready for checkin to Accepted

Patch no longer applies cleanly.

comment:9 Changed 2 years ago by jambonrose

  • Resolution set to duplicate
  • Status changed from assigned to closed

This is a duplicate of #16502. Please see that issue for resolution of this problem.

Note: See TracTickets for help on using tickets.
Back to Top