Opened 7 years ago

Closed 7 years ago

#28331 closed Cleanup/optimization (fixed)

Add an extra_context attribute to class-based generic views that use ContextMixin (e.g. TemplateView)

Reported by: Jeremy Owned by: Bruno Alla
Component: Generic views Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Jeremy)

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.

Change History (8)

comment:1 by Jeremy, 7 years ago

Description: modified (diff)

comment:2 by Jeremy, 7 years ago

Description: modified (diff)

comment:3 by Tim Graham, 7 years ago

Component: UncategorizedGeneric views
Easy pickings: set
Summary: extra_context argument on TemplateView.as_viewAdd an extra_context attribute to class-based generic views that use ContextMixin (e.g. TemplateView)
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

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 ContextMixin.get_context_data().

comment:4 by Bruno Alla, 7 years ago

Owner: changed from nobody to Bruno Alla
Status: newassigned

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

comment:5 by Tim Graham, 7 years ago

Has patch: set
Needs documentation: set

The PR is missing documentation. See the PatchReviewChecklist.

comment:6 by Bruno Alla, 7 years ago

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

comment:7 by Tim Graham, 7 years ago

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

comment:8 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In 604341c:

Fixed #28331 -- Added ContextMixin.extra_context to allowing passing context in as_view().

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