Skip to content

Fix IndexError in TemplateResponse #2909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions starlette/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ def TemplateResponse(self, *args: typing.Any, **kwargs: typing.Any) -> _Template
name = args[0]
context = args[1] if len(args) > 1 else kwargs.get("context", {})
status_code = args[2] if len(args) > 2 else kwargs.get("status_code", 200)
headers = args[2] if len(args) > 2 else kwargs.get("headers")
media_type = args[3] if len(args) > 3 else kwargs.get("media_type")
background = args[4] if len(args) > 4 else kwargs.get("background")
headers = args[3] if len(args) > 3 else kwargs.get("headers")
media_type = args[4] if len(args) > 4 else kwargs.get("media_type")
background = args[5] if len(args) > 5 else kwargs.get("background")

if "request" not in context:
raise ValueError('context must include a "request" key')
Expand Down
121 changes: 121 additions & 0 deletions tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,127 @@ def page(request: Request) -> Response:
spy.assert_called()


class TestTemplatesArgsOnly:
# MAINTAINERS: remove after 1.0
def test_name_and_context(self, tmpdir: Path, test_client_factory: TestClientFactory) -> None:
path = os.path.join(tmpdir, "index.html")
with open(path, "w") as file:
file.write("value: {{ a }}")
templates = Jinja2Templates(directory=str(tmpdir))

def page(request: Request) -> Response:
return templates.TemplateResponse(
"index.html",
{"a": "b", "request": request},
)

app = Starlette(routes=[Route("/", page)])
client = test_client_factory(app)
with pytest.warns(DeprecationWarning):
response = client.get("/")

assert response.text == "value: b" # context was rendered
assert response.status_code == 200

def test_status_code(self, tmpdir: Path, test_client_factory: TestClientFactory) -> None:
path = os.path.join(tmpdir, "index.html")
with open(path, "w") as file:
file.write("value: {{ a }}")
templates = Jinja2Templates(directory=str(tmpdir))

def page(request: Request) -> Response:
return templates.TemplateResponse(
"index.html",
{"a": "b", "request": request},
201,
)

app = Starlette(routes=[Route("/", page)])
client = test_client_factory(app)
with pytest.warns(DeprecationWarning):
response = client.get("/")

assert response.text == "value: b" # context was rendered
assert response.status_code == 201

def test_headers(self, tmpdir: Path, test_client_factory: TestClientFactory) -> None:
path = os.path.join(tmpdir, "index.html")
with open(path, "w") as file:
file.write("value: {{ a }}")
templates = Jinja2Templates(directory=str(tmpdir))

def page(request: Request) -> Response:
return templates.TemplateResponse(
"index.html",
{"a": "b", "request": request},
201,
{"x-key": "value"},
)

app = Starlette(routes=[Route("/", page)])
client = test_client_factory(app)
with pytest.warns(DeprecationWarning):
response = client.get("/")

assert response.text == "value: b" # context was rendered
assert response.status_code == 201
assert response.headers["x-key"] == "value"

def test_media_type(self, tmpdir: Path, test_client_factory: TestClientFactory) -> None:
path = os.path.join(tmpdir, "index.html")
with open(path, "w") as file:
file.write("value: {{ a }}")
templates = Jinja2Templates(directory=str(tmpdir))

def page(request: Request) -> Response:
return templates.TemplateResponse(
"index.html",
{"a": "b", "request": request},
201,
{"x-key": "value"},
"text/plain",
)

app = Starlette(routes=[Route("/", page)])
client = test_client_factory(app)
with pytest.warns(DeprecationWarning):
response = client.get("/")

assert response.text == "value: b" # context was rendered
assert response.status_code == 201
assert response.headers["x-key"] == "value"
assert response.headers["content-type"] == "text/plain; charset=utf-8"

def test_all_args(self, tmpdir: Path, test_client_factory: TestClientFactory) -> None:
path = os.path.join(tmpdir, "index.html")
with open(path, "w") as file:
file.write("value: {{ a }}")
templates = Jinja2Templates(directory=str(tmpdir))

spy = mock.MagicMock()

def page(request: Request) -> Response:
return templates.TemplateResponse(
"index.html",
{"a": "b", "request": request},
201,
{"x-key": "value"},
"text/plain",
BackgroundTask(func=spy),
)

app = Starlette(routes=[Route("/", page)])
client = test_client_factory(app)
with pytest.warns(DeprecationWarning):
response = client.get("/")

assert response.text == "value: b" # context was rendered
assert response.status_code == 201
assert response.headers["x-key"] == "value"
assert response.headers["content-type"] == "text/plain; charset=utf-8"
spy.assert_called()


def test_templates_when_first_argument_is_request(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
# MAINTAINERS: remove after 1.0
path = os.path.join(tmpdir, "index.html")
Expand Down