Skip to content

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

Open
jesustorres-code wants to merge 1 commit 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 1 commit 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
Member

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 on lines +84 to 88
"{% for item in results %}{{ item.name }}{% endfor %}"
)
raise TemplateDoesNotExist(template_name_list[0])

django.template.loader.get_template = get_template
Comment on lines +171 to +175
# data may be a list when a list view is used or when a ValidationError
# is raised; wrap it in a dict so Django's template engine can accept it.
# Use 'results' to stay consistent with paginated response conventions.
if isinstance(data, list):
return {'details': data, 'status_code': response.status_code}
return {'results': data, 'status_code': response.status_code}
@auvipy auvipy self-requested a review June 9, 2026 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants