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
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.

1 participant