Django ContextMixin object 'super' has no attribute 'get_context_data'

I need to pass some context to templates in multiple views. The context is obtained from BD using some custom information, so I implemented a specific ContextMixin class:

class CampaignContextMixin(ContextMixin):
"""
This mixin returns context with info related to user campaign.
It can be used in any view that needs campaign-related info to a template.
"""
def get_campaigns(self):
    # Get the first campaign related to user, can be more in the future
    return self.request.user.campaign_set.all()

# Method Overwritten to pass campaign data to template context
def get_context_data(self, **kwargs):
    context = super(CampaignContextMixin).get_context_data(**kwargs)
    campaign = self.get_campaigns()[0]
    context['campaign_name'] = campaign.name
    context['campaign_start_date'] = campaign.start_date
    context['campaign_end_date'] = campaign.end_date
    context['org_name'] = self.request.user.organization.name
    context['campaign_image'] = campaign.image.url
    context['campaign_details'] = campaign.details
    return context

      

Then I try to use it in my views, but I get an error:

'super' object has no attribute 'get_context_data'

class VoucherExchangeView(CampaignContextMixin, TemplateView):
"""
This view Handles the exchange of vouchers.
"""
template_name = "voucher_exchange.html"

def get_context_data(self, **kwargs):
    ctx = super(VoucherExchangeView).get_context_data(**kwargs)
    # add specific stuff if needed
    return ctx

      

I'm not sure if this is caused by an inheritance error or because TemplateView inherits from ContextMixin as well. My goal is to reuse code that adds campaign information to context.

thank

+3


source to share


1 answer


You meant



super(CampaignContextMixin, self).get_context_data(**kwargs)
#super().get_context_data(**kwargs) --> python3

      

+5


source







All Articles