小编Mar*_*ano的帖子

Django ContextMixin'super'对象没有属性'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'] = …
Run Code Online (Sandbox Code Playgroud)

python django django-views

4
推荐指数
1
解决办法
1618
查看次数

标签 统计

django ×1

django-views ×1

python ×1