Skip to content

Use 'results' key in TemplateHTMLRenderer when data is a list - #9972

Open
jesustorres-code wants to merge 7 commits into
encode:mainfrom
jesustorres-code:fix/template-html-renderer-results-key
Open

Use 'results' key in TemplateHTMLRenderer when data is a list#9972
jesustorres-code wants to merge 7 commits into
encode:mainfrom
jesustorres-code:fix/template-html-renderer-results-key

Conversation

@jesustorres-code

Copy link
Copy Markdown

Fixes #5236

Problem

TemplateHTMLRenderer.get_template_context() wraps list data (from list views and ValidationError) 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' in get_template_context().

Templates using TemplateHTMLRenderer on a list view should reference {{ results }} or iterate with {% for item in results %}.

Tests

  • Added test_list_view_with_template_html_renderer: end-to-end test of a list view rendered via TemplateHTMLRenderer using {{ results }}
  • Added 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 }}.

…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
@browniebroke

Copy link
Copy Markdown
Collaborator

I see , yes, that's unfortunate that we went with the wrong name...

Projects that upgraded after July 2024 (when #9467 landed) and are using {{ details }} in their templates will need to rename the variable to {{ results }}.

I wonder if we could do something to provide a fallback to make details work like results with a deprecation warning for a little while...

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 details to results in TemplateHTMLRenderer.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 not details).

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.

Comment thread tests/test_htmlrenderer.py
Comment thread rest_framework/renderers.py Outdated
@auvipy
auvipy self-requested a review June 9, 2026 11:34
@browniebroke

Copy link
Copy Markdown
Collaborator

I see , yes, that's unfortunate that we went with the wrong name...

Projects that upgraded after July 2024 (when #9467 landed) and are using {{ details }} in their templates will need to rename the variable to {{ results }}.

I wonder if we could do something to provide a fallback to make details work like results with a deprecation warning for a little while...

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 deprecation.py:

class RemovedInDRF319Warning(DeprecationWarning):
    pass


class RemovedInDRF320Warning(PendingDeprecationWarning):
    pass

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 _DeprecatedResultsList is 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 underlying data. Implement the wrapper as a proxy around the original data object.
    _warned = False

    def _warn(self):
        if not self._warned:
            self._warned = True

rest_framework/renderers.py:149

  • After turning _DeprecatedResultsList into 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__()

Comment thread rest_framework/renderers.py Outdated
Comment on lines +5 to +6
class RemovedInDRF320Warning(PendingDeprecationWarning):
pass
auvipy and others added 2 commits August 26, 2026 21:38
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

TemplateHTMLRenderer - TypeError - context must be a dict rather than ReturnList.

4 participants