Skip to content

Commit

Permalink
Fix DeprecationWarning: The name is not the first parameter anymore.
Browse files Browse the repository at this point in the history
Addresses jowilf#574

DeprecationWarning: The `name` is not the first parameter anymore. The first parameter should be the `Request` instance.
  Replace `TemplateResponse(name, {"request": request})` by `TemplateResponse(request, name)`.
    warnings.warn(

Before Starlette 0.29.0, the name was the first parameter.

Also, before that, in previous versions, the request object was passed as part of the key-value pairs in the context for Jinja2.

See:
- https://www.starlette.io/release-notes/#0290
- encode/starlette#2191
- https://github.com/encode/starlette/blob/c78c9aac17a4d68e0647252310044502f1b7da71/starlette/templating.py#L166-L178
- https://fastapi.tiangolo.com/reference/templating/#fastapi.templating.Jinja2Templates.TemplateResponse
- https://fastapi.tiangolo.com/advanced/templates/#using-jinja2templates
  • Loading branch information
ptrstn committed Aug 31, 2024
1 parent 9ee5829 commit 0638995
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
4 changes: 3 additions & 1 deletion docs/user-guide/getting-started/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ from starlette_admin import CustomView
class HomeView(CustomView):
async def render(self, request: Request, templates: Jinja2Templates) -> Response:
return templates.TemplateResponse(
"home.html", {"request": request, "latest_posts": ..., "top_users": ...}
request,
name="home.html",
context={"latest_posts": ..., "top_users": ...},
)


Expand Down
15 changes: 9 additions & 6 deletions starlette_admin/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,9 @@ async def render_login(self, request: Request, admin: "BaseAdmin") -> Response:
"""Render the default login page for username & password authentication."""
if request.method == "GET":
return admin.templates.TemplateResponse(
"login.html",
{"request": request, "_is_login_path": True},
request=request,
name="login.html",
context={"_is_login_path": True},
)
form = await request.form()
try:
Expand All @@ -251,14 +252,16 @@ async def render_login(self, request: Request, admin: "BaseAdmin") -> Response:
)
except FormValidationError as errors:
return admin.templates.TemplateResponse(
"login.html",
{"request": request, "form_errors": errors, "_is_login_path": True},
request=request,
name="login.html",
context={"form_errors": errors, "_is_login_path": True},
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
)
except LoginFailed as error:
return admin.templates.TemplateResponse(
"login.html",
{"request": request, "error": error.msg, "_is_login_path": True},
request=request,
name="login.html",
context={"error": error.msg, "_is_login_path": True},
status_code=HTTP_400_BAD_REQUEST,
)

Expand Down
42 changes: 26 additions & 16 deletions starlette_admin/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,9 @@ async def _render_list(self, request: Request) -> Response:
if not model.is_accessible(request):
raise HTTPException(HTTP_403_FORBIDDEN)
return self.templates.TemplateResponse(
model.list_template,
{
"request": request,
request=request,
name=model.list_template,
context={
"model": model,
"title": model.title(request),
"_actions": await model.get_all_actions(request),
Expand All @@ -395,9 +395,9 @@ async def _render_detail(self, request: Request) -> Response:
if obj is None:
raise HTTPException(HTTP_404_NOT_FOUND)
return self.templates.TemplateResponse(
model.detail_template,
{
"request": request,
request=request,
name=model.detail_template,
context={
"title": model.title(request),
"model": model,
"raw_obj": obj,
Expand All @@ -410,11 +410,15 @@ async def _render_create(self, request: Request) -> Response:
request.state.action = RequestAction.CREATE
identity = request.path_params.get("identity")
model = self._find_model_from_identity(identity)
config = {"request": request, "title": model.title(request), "model": model}
config = {"title": model.title(request), "model": model}
if not model.is_accessible(request) or not model.can_create(request):
raise HTTPException(HTTP_403_FORBIDDEN)
if request.method == "GET":
return self.templates.TemplateResponse(model.create_template, config)
return self.templates.TemplateResponse(
request=request,
name=model.create_template,
context=config,
)
form = await request.form()
dict_obj = await self.form_to_dict(request, form, model, RequestAction.CREATE)
try:
Expand All @@ -427,8 +431,9 @@ async def _render_create(self, request: Request) -> Response:
}
)
return self.templates.TemplateResponse(
model.create_template,
config,
request=request,
name=model.create_template,
context=config,
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
)
pk = await model.get_pk_value(request, obj)
Expand All @@ -452,14 +457,17 @@ async def _render_edit(self, request: Request) -> Response:
if obj is None:
raise HTTPException(HTTP_404_NOT_FOUND)
config = {
"request": request,
"title": model.title(request),
"model": model,
"raw_obj": obj,
"obj": await model.serialize(obj, request, RequestAction.EDIT),
}
if request.method == "GET":
return self.templates.TemplateResponse(model.edit_template, config)
return self.templates.TemplateResponse(
request=request,
name=model.edit_template,
context=config,
)
form = await request.form()
dict_obj = await self.form_to_dict(request, form, model, RequestAction.EDIT)
try:
Expand All @@ -472,8 +480,9 @@ async def _render_edit(self, request: Request) -> Response:
}
)
return self.templates.TemplateResponse(
model.edit_template,
config,
request=request,
name=model.edit_template,
context=config,
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
)
pk = await model.get_pk_value(request, obj)
Expand All @@ -493,8 +502,9 @@ async def _render_error(
) -> Response:
assert isinstance(exc, HTTPException)
return self.templates.TemplateResponse(
"error.html",
{"request": request, "exc": exc},
request=request,
name="error.html",
context={"exc": exc},
status_code=exc.status_code,
)

Expand Down
4 changes: 3 additions & 1 deletion starlette_admin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ def __init__(
async def render(self, request: Request, templates: Jinja2Templates) -> Response:
"""Default methods to render view. Override this methods to add your custom logic."""
return templates.TemplateResponse(
self.template_path, {"request": request, "title": self.title(request)}
request=request,
name=self.template_path,
context={"title": self.title(request)},
)

def is_active(self, request: Request) -> bool:
Expand Down

0 comments on commit 0638995

Please sign in to comment.