• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

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

I need to pass some context to templates in several views. Context is obtained from the BD using some user's info, so I've implemented a Specific ContextMixin class:

class CampaignContextMixin(ContextMixin):
"""
This mixin returns context with info related to user's 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'm trying to use it in my views, but I'm getting 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' not sure if is caused because inheritance error, or because TemplateView inherits also from ContextMixin. My goal is to reuse the code that adds the campaigns info to the context.

Thanks

over 3 years ago · Santiago Trujillo
1 answers
Answer question

0

Did you mean

super(CampaignContextMixin, self).get_context_data(**kwargs)
#super().get_context_data(**kwargs) --> python3
over 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error