get_context_data() considered harmful


Hello everybody <3

Let’s talk about ideas for deprecation / discouraging the use of get_context_data().

get_context_data() is useful to support legacy templates from the pre-CBV era, since CBV we can now access any attributes from the view instance with {{ view }}.

I’ve seen things like this in many projects, I wonder if it’s the case for other people too:

def get_context_data(self, **kwargs):
    c = super().get_context_data(**kwargs)
    c['something'] = self.something
    return c

And I’m guilty as charged you can find commits from myself like this all over internet.

Do you think this is really necessary and shouldn’t we encourage users to take a leap of faith in OOP and call {{ view.something }} instead in the templates ? Isn’t a view instance the right holder any attribute that’s specific to this request in this view ?

Another thing that seems a bit weird is that a builtin CBV requires two calls to render a template and enforces dealing with the context, as we can see in Django’s TemplateView:

class TemplateView(TemplateResponseMixin, ContextMixin, View):
    """
    Render a template. Pass keyword arguments from the URLconf to the context.
    """
    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        return self.render_to_response(context)

So, there’s not really a public API to render to response with the default context, either call TemplateView.get() directly either be forced to cast a context.

I’m hoping this will work long enough:

return generic.TemplateView.get(self, request, *args, **kwargs)

But really this should maybe be:

return self.render_to_response()

That’s it !

Have fun ;)

– ∞