Opened 4 years ago

Closed 3 years ago

#18853 closed Bug (duplicate)

get_template_names() does not consider the queryset-only case

Reported by: Melvyn Sopacua Owned by: Florian Apolloner
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 Melvyn Sopacua 4 years ago.
Fix and testcase

Download all attachments as: .zip

Change History (10)

Changed 4 years ago by Melvyn Sopacua

Fix and testcase

comment:1 Changed 4 years ago by Simon Charette

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 4 years ago by Melvyn Sopacua

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 4 years ago by Jason Mayfield

Cc: jason@… added
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

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

comment:4 Changed 4 years ago by Jason Mayfield

Owner: changed from nobody to Jason Mayfield
Status: newassigned
Triage Stage: AcceptedReady for checkin

comment:5 Changed 4 years ago by Jason Mayfield

Owner: changed from Jason Mayfield to nobody
Status: assignednew

comment:6 Changed 4 years ago by bhuztez

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

comment:7 Changed 4 years ago by Florian Apolloner

Owner: changed from nobody to Florian Apolloner
Status: newassigned

comment:8 Changed 3 years ago by Tim Graham

Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

Patch no longer applies cleanly.

comment:9 Changed 3 years ago by jambonrose

Resolution: duplicate
Status: assignedclosed

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