[英]Django ContextMixin 'super' object has no attribute 'get_context_data'
我需要在幾個視圖中將一些上下文傳遞給模板。 使用某些用戶的信息從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
然后我試圖在我的視圖中使用它,但我收到一個錯誤:
'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
我不確定是否是因為繼承錯誤引起的,或者因為TemplateView也繼承自ContextMixin。 我的目標是重用將廣告系列信息添加到上下文的代碼。
謝謝
你的意思是
super(CampaignContextMixin, self).get_context_data(**kwargs)
#super().get_context_data(**kwargs) --> python3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.