我需要将一些上下文传递给多个视图中的模板。上下文是使用一些用户的信息从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
然后我试图在我的视图中使用它,但是我得到了一个错误:
^{pr2}$'super' object has no attribute 'get_context_data'
我不确定是因为继承错误,还是因为TemplateView继承了ContextMixin。我的目标是重用将活动信息添加到上下文中的代码。在
谢谢
你是说
相关问题 更多 >
编程相关推荐