Reputation: 315
I know that there are several generic views like ListView
, DetailView
, or simply View
.
The thing is can I actually get the context data that are declared in a BaseMixin
's get_context_data()
and use it in a View that doesn't have override get_context_data()
?
Example:
class BaseMixin(object):
def get_context_data(self, *args, **kwargs):
context = super(BaseMixin, self).get_context_data(**kwargs)
context['test'] = 1
return context
And the view that extends this BaseMixin
:
class FooView(BaseMixin, View):
def foo(self, request):
context = super(BaseMixin, self).get_context_data(**kwargs)
# do something
return
This is not working actually, even after put **kwargs
as a parameter in foo()
. The error is 'super' object has no attribute 'get_context_data'
.
So is there a way to get context data which was set in BaseMixin
in FooView
?
Thanks for your answers :)
Upvotes: 1
Views: 1841
Reputation: 315
Thanks to @Sayes and all answer posters, I finally solved this problem.
From what I figured out, the problem is actually in BaseMixin
, the inherited class of BaseMixin
, which is object
, doesn't have a get_context_data()
function, just like @Sayes commented.
After replace this object
with ContextMixin
, everything works perfectly, at least perfectly for now.
Here is the modified BaseMixin
:
class BaseMixin(ContextMixin):
def get_context_data(self, *args, **kwargs):
# do something
return context
Upvotes: 3