Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#26253 closed Bug (fixed)

Deprecation for TemplateResponse and SimpleTemplateResponse accepting django.template.Template is broken

Reported by: David Reitter Owned by: Tim Graham
Component: Template system Version: 1.8
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Tim Graham)

When TemplateResponse or SimpleTemplateResponse are called, a TypeError is thrown.

This is because their __init__() creates a BackendTemplate` giving only one argument to the constructor.

The resulting error is obviously confusing and obfuscates the warning.

The attached patch addresses the issue, although I am unsure whether one should assume, by default, that a DjangoTemplate was given.

Attachments (1)

response.patch (2.5 KB) - added by David Reitter 4 years ago.

Download all attachments as: .zip

Change History (12)

Changed 4 years ago by David Reitter

Attachment: response.patch added

comment:1 Changed 4 years ago by Tim Graham

Description: modified (diff)
Summary: TemplateResponse and BasicTemplateResponse: error message causes TypeErrorTemplateResponse and SimpleTemplateResponse: error message causes TypeError

Can you provide code to reproduce the issue? I'm not entirely clear on it based on the description. Is the issue relevant on master or only in 1.9 that has the deprecation path? If only 1.9, the problem may not be severe enough to warrant being patched there.

I edited the description to change BaseTemplateResponse to SimpleTemplateResponse since there's no class in Django with the former name. Please edit again if it should be something else.

comment:2 Changed 4 years ago by David Reitter

Yes, that change of yours is correct (my mistake).

The code that caused it is really quite simple. It just makes a TemplateResponse based on Template without the engine. This came up because I transitioned a Django 1.5 project to 1.9.

The code that produces the exception seems to be meant to handle outdated code like this.

from django.template import Template
from django.template.response import TemplateResponse

def results(request):
       content = "…"
       t = Template(content)
       return TemplateResponse(request, t)

comment:3 Changed 4 years ago by Tim Graham

Keywords: Teamplates removed
Needs tests: set
Severity: NormalRelease blocker
Summary: TemplateResponse and SimpleTemplateResponse: error message causes TypeErrorDeprecation for TemplateResponse and SimpleTemplateResponse accepting django.template.Template is broken
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug
Version: 1.91.8

Oh, I had trouble understanding your patch and the problem. The issue is that the backwards compatibility shim crashes. If you are able to add tests for the patch and send a pull request, that would be great.

from django.template import Template
from django.template.response import TemplateResponse
from django.http import HttpRequest

request = HttpRequest()
content = "…"
t = Template(content)
response = TemplateResponse(request, t)
...
  File "/home/tim/code/django/django/template/response.py", line 28, in __init__
    template = BackendTemplate(template)
TypeError: __init__() missing 1 required positional argument: 'backend'

comment:4 Changed 4 years ago by David Reitter

Sorry, I looked into writing a test and pretty much got stuck with what seemed to be the test scripts running the system install of Django rather than the code in the checkout.
I won’t have the time to submit more than the patch I submitted, due to various professional obligations. Apologies.

comment:5 Changed 4 years ago by Tim Graham

Owner: changed from nobody to Tim Graham
Status: newassigned

comment:6 Changed 4 years ago by Tim Graham

Needs tests: unset

comment:7 Changed 4 years ago by Simon Charette

Triage Stage: AcceptedReady for checkin

comment:8 Changed 4 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 3fedfc45:

[1.9.x] Fixed #26253 -- Fixed crashing deprecation shims in SimpleTemplateResponse.

Thanks David Reitter for the report and initial patch.

comment:9 Changed 4 years ago by Tim Graham <timograham@…>

In 061a7ff3:

[1.8.x] Refs #26253 -- Added tests for deprecation shims in SimpleTemplateResponse.

Backport of 3fedfc452fa94f8a6c9a64289d00202313ceb564 from stable/1.9.x

comment:10 Changed 4 years ago by Tim Graham <timograham@…>

In c6ab81db:

[1.9.x] Refs #26253 -- Amended release note as this issue doesn't affect 1.8.

comment:11 Changed 4 years ago by Tim Graham <timograham@…>

In 33a4040d:

Refs #26253 -- Forwardported release note.

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