Opened 13 years ago

Last modified 12 years ago

#16744 closed New feature

Class based view should have the view object in the context — at Version 2

Reported by: Reinout van Rees Owned by: nobody
Component: Generic views Version: 1.3
Severity: Normal Keywords:
Cc: jonas-django@…, taavi@…, reinout@…, jeroen@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Jannis Leidel)

Summary: your_cbv_object.get_context_data() should include {'view': self}.

Now that django has class based views, I was surprised that I don't get the view object in my template's context. And that I still have to hand-craft my context dictionary.

What would be handier than setting attributes or adding methods to the view class and accessing them with {{ view.my_attribute }} and {{ view.my_method }}`? Without having to do the double work of adding them by hand to the context dictionary? An additional benefit: you encourage to do even more in python and even less in the template by making it easy to add helper methods in your view.

The solution could as simple as adding {'view': self} in the three or four spots in django itself where a .get_context_data() is defined.

Change History (2)

comment:1 by Reinout van Rees, 13 years ago

As background, here's what Russ Magee said on the mailinglist about this after I asked about it:

To the best of my knowledge, the only reason the view isn't included
in the template context is because over more than two years of design
discussion, I don't think the idea of including the view in the
context was ever proposed.

It seems like a reasonable idea to me, though, and it should be
possible to accommodate in a backwards compatible fashion. The trivial
fix would be to add a 'view' variable to the default context. It might
also be possible to replace the default get_context_data
implementation with something that reflects the attributes of the view
object itself -- however, this will obviously require a bit more
design work to make sure there aren't any backwards incompatible
implications.

comment:2 by Jannis Leidel, 13 years ago

Description: modified (diff)
Triage Stage: UnreviewedAccepted

Corrected get_context_dict to get_context_data.

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