Opened 4 years ago

Closed 3 years ago

#16642 closed Bug (invalid)

TemplateView get_context_data returns params dict

Reported by: martinogden Owned by: martinogden
Component: Generic views Version: 1.3
Severity: Normal Keywords: class-based generic view, TemplateView
Cc: martinogden Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

the TemplateResponseView class based view should return kwargs as a dict when calling get_context_data (same as all the other class based views), however it returns the kwargs as {'params': kwargs} therefore they are extra context is namespaced (e.g. {{ params.extra_data }} rather than {{ extra_data }} as documented.

Attachments (1)

TemplateView.diff (492 bytes) - added by martinogden 4 years ago.

Download all attachments as: .zip

Change History (7)

Changed 4 years ago by martinogden

comment:1 Changed 4 years ago by martinogden

  • Cc martinogden added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 4 years ago by martinogden

  • Summary changed from TemplateResponseView get_context_data returns params dict to TemplateView get_context_data returns params dict

comment:3 Changed 4 years ago by martinogden

  • Keywords class-based generic view TemplateView added

typo in description - TemplateView not TemplateResponseView

comment:4 Changed 4 years ago by aaugustin

  • Resolution set to invalid
  • Status changed from new to closed

TemplateView behaves according to the documentation. params is intended to contain the parameters captured in the URL.

See https://docs.djangoproject.com/en/dev/ref/class-based-views/#django.views.generic.base.TemplateView

comment:5 Changed 3 years ago by simon@…

  • Resolution invalid deleted
  • Status changed from closed to reopened

This behaviour seems rather undesirable. The TemplateView is meant to be the quickest and easiest way of rendering a template. Why does it encapsulate the kwargs inside a redundant single-element dictionary? Having to change the templates to include params feels totally wrong.

Is there an alternative class-based view that doesn't require the use of 'params'?

comment:6 Changed 3 years ago by russellm

  • Resolution set to invalid
  • Status changed from reopened to closed

The params dictionary isn't redundant at all -- it's a namespace. The parameters passed in to the view aren't the only context variables that the template can render. Consider the case where the arguments passed in part of the URL pattern overlap with context variables created by the view.

Note: See TracTickets for help on using tickets.
Back to Top