[英]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: 使用某些用户的信息从BD获取上下文,因此我实现了一个特定的ContextMixin类:
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' 'super'对象没有属性'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. 我不确定是否是因为继承错误引起的,或者因为TemplateView也继承自ContextMixin。 My goal is to reuse the code that adds the campaigns info to the context. 我的目标是重用将广告系列信息添加到上下文的代码。
Thanks 谢谢
Did you mean 你的意思是
super(CampaignContextMixin, self).get_context_data(**kwargs)
#super().get_context_data(**kwargs) --> python3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.