I just started to shift from function-based views to class-based views, as I realize more and more the benefits of the latter, especially regarding the modularity that you can achieve via mixins.
However, as CBVs and Mixins are still quite new to me, I wanted to check, if I understand the basic concepts correctly:
Example:
class ReportingMixin1(object):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['reportingmixin1_context'] = 'test1'
return context
class ReportingMixin2(object):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['reportingmixin2_context'] = 'test2'
return context
class MyTemplateView(ReportingMixin1, ReportingMixin2, TemplateView):
template_name = 'test4.html'
My questions are the following:
- With function based views I would do
context = {'reportingmixin1': 'test1}
but I see that with class based views this is always done viacontext['reportingmixin1'] = 'test1
. Why is this the case? I assume one reason is that when combining multiple mixins in a TemplateView (as in the code in my example) but withcontext = {'reportingmixin1': 'test1}
andcontext = {'reportingmixin2': 'test2}
I would initiate or redefine the context dictionary in each mixin, so you would have only access to the context keys I defined in my mixin that is called last? - What is
context = super().get_context_data(**kwargs)
doing exactly? I read that it is "calling the base implementation first to get a context" - but what does that mean exactly? - When I combine multiple Mixins in my Template view in the way shown in my code example in
MyTemplateView
, I can access all the context keys in my template form all my mixins, correct?