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 1 commit 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.
| "{% for item in results %}{{ item.name }}{% endfor %}" | ||
| ) | ||
| raise TemplateDoesNotExist(template_name_list[0]) | ||
|
|
||
| django.template.loader.get_template = get_template |
| # 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} |
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 }}.