Fix Starlette 1.0.0 TemplateResponse API compatibility#84
Open
vdaluz wants to merge 1 commit intosam1am:mainfrom
Open
Fix Starlette 1.0.0 TemplateResponse API compatibility#84vdaluz wants to merge 1 commit intosam1am:mainfrom
vdaluz wants to merge 1 commit intosam1am:mainfrom
Conversation
Starlette 1.0.0 changed the TemplateResponse calling convention.
The old positional API passed the template name first and a dict
containing the request object:
TemplateResponse("name.html", {"request": request, ...})
The new API takes the request as the first argument and removes it
from the context dict:
TemplateResponse(request, "name.html", {...})
The old style causes a TypeError at runtime with Starlette 1.0.0+,
breaking every page that renders an HTML response. Updated all 14
TemplateResponse calls across the five route files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
requirements.txtspecifiesfastapianduvicornwithout version pins. FastAPI bundles Starlette as a dependency, and Starlette 1.0.0 (released September 2024) introduced a breaking change toTemplateResponse. New installs now resolve to Starlette 1.0.0+ and the app might be broken out of the box with a 500 on every HTML page.What breaks
The old
TemplateResponseAPI passed the template name first and embeddedrequestin the context dict:Starlette 1.0.0 changed the signature —
requestis now the first positional argument and is no longer part of the context dict:The old style raises
TypeError: unhashable type: 'dict'at runtime because Jinja2's template LRU cache receives the context dict where it expects the template name string.Fix
Updates all 14
TemplateResponsecalls acrossapp_auth.py,library.py,discover.py,collections.py, andsettings.pyto use the new API.