Use 'results' key in TemplateHTMLRenderer when data is a list - #9972
Use 'results' key in TemplateHTMLRenderer when data is a list#9972jesustorres-code wants to merge 5 commits into
Conversation
…plateHTMLRenderer When TemplateHTMLRenderer.get_template_context() receives list data (from a list view or a ValidationError), it wraps it in a dict so Django's template engine can accept it. The key 'results' is more consistent with DRF's paginated response convention than 'details'. Fixes encode#5236
|
I see , yes, that's unfortunate that we went with the wrong name...
I wonder if we could do something to provide a fallback to make |
There was a problem hiding this comment.
Pull request overview
Adjusts TemplateHTMLRenderer.get_template_context() so list-shaped response data is exposed to templates under a results key (instead of details), aligning with DRF’s paginated response convention and resolving list-context rendering errors.
Changes:
- Rename list-wrapping context key from
detailstoresultsinTemplateHTMLRenderer.get_template_context(). - Add an end-to-end HTML rendering test for a list view using
{{ results }}. - Add a unit test asserting list data is wrapped under
results(and notdetails).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
rest_framework/renderers.py |
Renames the list-context key to results in TemplateHTMLRenderer.get_template_context(). |
tests/test_htmlrenderer.py |
Adds list-view HTML rendering coverage and a unit test for the new results context key. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
I think that might work: import warnings
from rest_framework.deprecation import RemovedInDRF320Warning
class ResultDetailsFallback(dict):
def __getitem__(self, item):
if item == 'details':
warnings.warn(
'The "detail" key is deprecated, update your templates to use "results" instead.',
RemovedInDRF320Warning,
stacklevel=2,
)
return super().__getitem__('results')
return super().__getitem__(item)
def __contains__(self, item):
if item == 'details':
return super().__contains__('results')
return super().__contains__(item)
...
if isinstance(data, list):
return ResultDetailsFallback(**{'results': data, 'status_code': response.status_code})
...And class RemovedInDRF319Warning(DeprecationWarning):
pass
class RemovedInDRF320Warning(PendingDeprecationWarning):
pass |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (2)
rest_framework/renderers.py:127
- Even if
_DeprecatedResultsListis kept, it should proxy the original list rather than behaving like an independent list instance; otherwise operations like iteration/length/indexing (and potentially membership tests) may reflect the wrapper's own internal list state instead of the underlyingdata. Implement the wrapper as a proxy around the originaldataobject.
_warned = False
def _warn(self):
if not self._warned:
self._warned = True
rest_framework/renderers.py:149
- After turning
_DeprecatedResultsListinto a proxy,__repr__should also delegate to the underlying data; otherwise debugging/logging will show the proxy object rather than the wrapped list contents.
def __repr__(self):
self._warn()
return super().__repr__()
| return ret.encode() | ||
|
|
||
|
|
||
| class _DeprecatedResultsList(list): |
| class RemovedInDRF320Warning(PendingDeprecationWarning): | ||
| pass |
Fixes #5236
Problem
TemplateHTMLRenderer.get_template_context()wraps list data (from list views andValidationError) in a dict using'details'as the key. This was introduced in #9467.The key
'details'is inconsistent with DRF's own paginated response convention which uses'results'. Maintainer @lovelydinosaur explicitly requested'results'in the issue thread, and contributor @auvipy agreed in PR #8569.Change
One-line rename:
'details'→'results'inget_template_context().Templates using
TemplateHTMLRendereron a list view should reference{{ results }}or iterate with{% for item in results %}.Tests
test_list_view_with_template_html_renderer: end-to-end test of a list view rendered viaTemplateHTMLRendererusing{{ results }}test_get_template_context_wraps_list_under_results_key: unit test that asserts the context dict uses'results'and not'details'Migration note
Projects that upgraded after July 2024 (when #9467 landed) and are using
{{ details }}in their templates will need to rename the variable to{{ results }}.