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.
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>
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>
* 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>
* 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>
* 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>
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>
Comment (by Tim Graham):
Looks good. ref/class-based-views/flattened-index/ also.
--
Ticket URL: <https://code.djangoproject.com/ticket/28331#comment:7>
* 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>