Skip to content

Update TemplateResponse to use newer version signature and syntax #63

Description

@jsjiang

Before FastAPI 0.108.0), name was the first positional parameter, and the request had to be manually jammed inside the context dictionary. In newer FastAPI/Starlette versions, TemplateResponse enforces request as the first argument, and request, name, and context should be passed explicitly as keyword arguments (ref: https://fastapi.tiangolo.com/advanced/templates/#using-jinja2templates), such as:

app = FastAPI()
templates = Jinja2Templates(directory="templates")

@app.get("/items/{id}")
async def read_item(request: Request, id: str):
    return templates.TemplateResponse(
        request=request,
        name="item.html",
        context={"id": id}
    )

Running current code such as templates.TemplateResponse("index.html", {"request": request}) will trigger type error TypeError: unhashable type: 'dict' causing 500 internal server error.

Action items:

  • Update TemplateResponse to use the newer version signature and syntax
  • Run unit tests
  • Test Resolver with a development instance using uvicorn

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions