[Django] #28331: extra_context argument on TemplateView.as_view

64 views
Skip to first unread message

Django

unread,
Jun 21, 2017, 3:05:34 PM6/21/17
to django-...@googlegroups.com
#28331: extra_context argument on TemplateView.as_view
-----------------------------------------+------------------------
Reporter: Jeremy | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.11
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
It used to be possible to pass extra_context to direct_to_template. Then
direct_to_template was replaced with TemplateView.as_view ... but the
latter function never got the ability to take an extra_context arg.

As a result there are now half a trillion (rough estimate) re-
implementations of the following:


{{{
class StaticPageView(TemplateView):
context = None

def get_context_data(self, **kwargs):
context = super(StaticPageView, self).get_context_data(**kwargs)
context.update(self.context or {})
return context


}}}
You can find them across the web, across StackOverflow, etc. Everyone has
to make this one stupid class just so that they can pass an extra context
to template-only views.

This is counter-productive: the whole point of a framework is to *prevent*
everyone from having to re-invent the same code. Adding yet another re-
implementation of StaticPageView does nothing to improve the code, it only
clutters it up with boilerplate.

Please fix this by allowing as_view to take an extra_context arg

--
Ticket URL: <https://code.djangoproject.com/ticket/28331>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 21, 2017, 8:02:34 PM6/21/17
to django-...@googlegroups.com
#28331: extra_context argument on TemplateView.as_view
-------------------------------+--------------------------------------

Reporter: Jeremy | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.11
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by Jeremy:

Old description:

> It used to be possible to pass extra_context to direct_to_template. Then
> direct_to_template was replaced with TemplateView.as_view ... but the
> latter function never got the ability to take an extra_context arg.
>
> As a result there are now half a trillion (rough estimate) re-
> implementations of the following:
>

> {{{
> class StaticPageView(TemplateView):
> context = None
>
> def get_context_data(self, **kwargs):
> context = super(StaticPageView, self).get_context_data(**kwargs)
> context.update(self.context or {})
> return context
>

> }}}
> You can find them across the web, across StackOverflow, etc. Everyone
> has to make this one stupid class just so that they can pass an extra
> context to template-only views.
>
> This is counter-productive: the whole point of a framework is to
> *prevent* everyone from having to re-invent the same code. Adding yet

> another re-implementation of StaticPageView does nothing to improve the


> code, it only clutters it up with boilerplate.
>
> Please fix this by allowing as_view to take an extra_context arg

New description:

It used to be possible to pass extra_context to direct_to_template. Then
direct_to_template was replaced with TemplateView.as_view ... but the
latter function never got the ability to take an extra_context arg.

As a result there are now half a trillion (rough estimate) re-
implementations of the following:


{{{
class StaticPageView(TemplateView):
context = None

def get_context_data(self, **kwargs):
context = super(StaticPageView, self).get_context_data(**kwargs)
context.update(self.context or {})
return context


}}}
You can find them across the web, across StackOverflow, etc. Everyone has

to remake this one small class just so that they can pass an extra context
to template-only views.

This is counter-productive: the whole point of a framework is to *prevent*
everyone from having to re-invent the same code. Adding yet another re-
implementation of StaticPageView does nothing to improve the code, it only
clutters it up with boilerplate.

Please fix this by allowing as_view (ideally in all generic views) to take
an extra_context arg.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/28331#comment:1>

Django

unread,
Jun 21, 2017, 8:03:35 PM6/21/17
to django-...@googlegroups.com
#28331: extra_context argument on TemplateView.as_view
-------------------------------+--------------------------------------

Reporter: Jeremy | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.11
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by Jeremy:

Old description:

> It used to be possible to pass extra_context to direct_to_template. Then


> direct_to_template was replaced with TemplateView.as_view ... but the
> latter function never got the ability to take an extra_context arg.
>
> As a result there are now half a trillion (rough estimate) re-
> implementations of the following:
>

> {{{
> class StaticPageView(TemplateView):
> context = None
>
> def get_context_data(self, **kwargs):
> context = super(StaticPageView, self).get_context_data(**kwargs)
> context.update(self.context or {})
> return context
>

> }}}
> You can find them across the web, across StackOverflow, etc. Everyone

> has to remake this one small class just so that they can pass an extra


> context to template-only views.
>
> This is counter-productive: the whole point of a framework is to
> *prevent* everyone from having to re-invent the same code. Adding yet

> another re-implementation of StaticPageView does nothing to improve the


> code, it only clutters it up with boilerplate.
>

> Please fix this by allowing as_view (ideally in all generic views) to
> take an extra_context arg.

New description:

It used to be possible to pass extra_context to direct_to_template. Then
direct_to_template was replaced with TemplateView.as_view ... but the
latter function never got the ability to take an extra_context arg.

As a result there are now many many re-implementations of some variation
of the following:


{{{
class StaticPageView(TemplateView):
context = None

def get_context_data(self, **kwargs):
context = super(StaticPageView, self).get_context_data(**kwargs)
context.update(self.context or {})
return context


}}}
You can find them across the web, across StackOverflow, etc. Everyone has

to remake this one small class just so that they can pass an extra context
to template-only views.

This is counter-productive: the whole point of a framework is to *prevent*
everyone from having to re-invent the same code. Adding yet another re-
implementation of StaticPageView does nothing to improve the code, it only
clutters it up with boilerplate.

Please fix this by allowing as_view (ideally in all generic views) to take
an extra_context arg.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/28331#comment:2>

Django

unread,
Jun 22, 2017, 9:05:24 AM6/22/17
to django-...@googlegroups.com
#28331: Add an extra_context attribute to class-based generic views that use
ContextMixin (e.g. TemplateView)
--------------------------------------+------------------------------------
Reporter: Jeremy | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Generic views | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham):

* type: Uncategorized => Cleanup/optimization
* component: Uncategorized => Generic views
* easy: 0 => 1
* stage: Unreviewed => Accepted


Comment:

This was also suggested in #17697 but the ticket triagers didn't
understand the suggestion. The suggestion seems okay to me. I think it
would involve modifying
[https://github.com/django/django/blob/8cb1b1fd8e529d1896daeb089ea726109e0ba4f7/django/views/generic/base.py#L22-L25
ContextMixin.get_context_data()].

--
Ticket URL: <https://code.djangoproject.com/ticket/28331#comment:3>

Django

unread,
Jun 23, 2017, 3:17:57 PM6/23/17
to django-...@googlegroups.com
#28331: Add an extra_context attribute to class-based generic views that use
ContextMixin (e.g. TemplateView)
-------------------------------------+-------------------------------------
Reporter: Jeremy | Owner: Bruno
Type: | Alla
Cleanup/optimization | Status: assigned

Component: Generic views | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Bruno Alla):

* status: new => assigned
* owner: nobody => Bruno Alla


Comment:

I'm going to give this a go (unless someone already started).

--
Ticket URL: <https://code.djangoproject.com/ticket/28331#comment:4>

Django

unread,
Jun 26, 2017, 8:16:14 PM6/26/17
to django-...@googlegroups.com
#28331: Add an extra_context attribute to class-based generic views that use
ContextMixin (e.g. TemplateView)
-------------------------------------+-------------------------------------
Reporter: Jeremy | Owner: Bruno
Type: | Alla
Cleanup/optimization | Status: assigned
Component: Generic views | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* needs_docs: 0 => 1
* has_patch: 0 => 1


Comment:

The [https://github.com/django/django/pull/8671 PR] is missing
documentation. See the PatchReviewChecklist.

--
Ticket URL: <https://code.djangoproject.com/ticket/28331#comment:5>

Django

unread,
Jun 27, 2017, 3:28:38 AM6/27/17
to django-...@googlegroups.com
#28331: Add an extra_context attribute to class-based generic views that use
ContextMixin (e.g. TemplateView)
-------------------------------------+-------------------------------------
Reporter: Jeremy | Owner: Bruno
Type: | Alla
Cleanup/optimization | Status: assigned
Component: Generic views | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Bruno Alla):

I assume a good place to document that is here:
https://docs.djangoproject.com/en/dev/ref/class-based-views/mixins-
simple/#contextmixin

As well as adding an entry in the release notes:
https://docs.djangoproject.com/en/dev/releases/2.0/

And maybe mention it in the TemplateView, under the context section as
well?
https://docs.djangoproject.com/en/dev/ref/class-based-
views/base/#templateview

--
Ticket URL: <https://code.djangoproject.com/ticket/28331#comment:6>

Django

unread,
Jun 27, 2017, 10:05:26 AM6/27/17
to django-...@googlegroups.com
#28331: Add an extra_context attribute to class-based generic views that use
ContextMixin (e.g. TemplateView)
-------------------------------------+-------------------------------------
Reporter: Jeremy | Owner: Bruno
Type: | Alla
Cleanup/optimization | Status: assigned
Component: Generic views | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham):

Looks good. ref/class-based-views/flattened-index/ also.

--
Ticket URL: <https://code.djangoproject.com/ticket/28331#comment:7>

Django

unread,
Jul 6, 2017, 10:35:10 AM7/6/17
to django-...@googlegroups.com
#28331: Add an extra_context attribute to class-based generic views that use
ContextMixin (e.g. TemplateView)
-------------------------------------+-------------------------------------
Reporter: Jeremy | Owner: Bruno
Type: | Alla
Cleanup/optimization | Status: closed

Component: Generic views | Version: 1.11
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"604341c85fe42c809d17ea2418566a48f60f78db" 604341c]:
{{{
#!CommitTicketReference repository=""
revision="604341c85fe42c809d17ea2418566a48f60f78db"
Fixed #28331 -- Added ContextMixin.extra_context to allowing passing
context in as_view().
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28331#comment:8>

Reply all
Reply to author
Forward
0 new messages