Necesito pasar algo de contexto a las plantillas en varias vistas. El contexto se obtiene de la BD utilizando la información de algunos usuarios, por lo que implementé una clase ContextMixin específica:
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
Luego estoy tratando de usarlo en mis vistas, pero recibo un error:
El objeto 'super' no tiene atributo '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
No estoy seguro si se debe a un error de herencia o porque TemplateView hereda también de ContextMixin. Mi objetivo es reutilizar el código que agrega la información de las campañas al contexto.
Gracias
Querías decir
super(CampaignContextMixin, self).get_context_data(**kwargs) #super().get_context_data(**kwargs) --> python3