Opened 7 years ago

Last modified 7 years ago

#28331 closed Cleanup/optimization

extra_context argument on TemplateView.as_view — at Version 1

Reported by: Jeremy Owned by: nobody
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 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.

Change History (1)

comment:1 by Jeremy, 7 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top