【问题标题】:Extending class, date based generic views in django and get_context_data?在 django 和 get_context_data 中扩展类、基于日期的通用视图?
【发布时间】:2011-07-29 08:51:20
【问题描述】:

我正在尝试使用我自己的 mixin“BaseViewMixin”扩展一个简单的基于日期的视图(使用 1.3 通用类方法):

class BaseViewMixin(object):
    """define some basic context for our views"""

    model = Alert
    month_format = "%m"
    date_field = "date"

    def get_context_data(self, **kwargs):
        """extra context"""
        context = super(BaseViewMixin, self).get_context_data(**kwargs)
        context["CACHE_SERVERS"] = settings.CACHE_SERVERS
        return context


    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(BaseViewMixin, self).dispatch(*args, **kwargs)

class IndexView(BaseViewMixin, TodayArchiveView):

    template_name= "index.html"

    def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)
        queryset = Alert.objects.default(self.day)
        tickets = Alert.objects.tickets(self.day)
        alert_groups = []
        for item in tickets:
            alert_groups.append({"ticket": item, "alerts": queryset.filter(ticket=item["ticket"])})
        context["alert_groups"] = alert_groups
        return context

问题是,一旦我为我的 IndexView 类覆盖 get_context_data 方法,您通常在上下文中获得的所有基于日期的内容都会被清除。这是为什么?我希望 {{ day }}、{{ previous_day }} 等会出现在上下文中,以及 self.当我删除我的 get_context_data 方法时,所有通用日期的东西都有效。

urls.py 条目只是:

url(r'^$', IndexView.as_view(), name='index'),

【问题讨论】:

  • {{ day }}, {{ previous_day }} 它们在哪里定义? TodayArchiveView?
  • TodayArchiveView 应该提供与DayArchiveView 相同的行为。在我的示例类中,我使用的 self.day 应该与 context["day"] 的工作方式相同,后者在 index.html 中可用。问题是 self.day 和 context["day"] 都没有。感谢您的评论!
  • 似乎与this bug 相关。让我发疯..

标签: django django-views


【解决方案1】:

这是一种允许我重用代码的方法。我仍然不明白为什么get_context_data 会以我在原始问题中定义的方式消除TodayArchiveView 上下文。似乎使用下面的场景会对我的 mixin 做同样的事情,但事实并非如此。在 DateMixin 中调用 get_context_data 时会保留 ViewMixin 上下文。

class ViewMixin(object):
    """define some basic context for our views"""

    model = Alert

    def get_context_data(self, **kwargs):
        """extra context"""
        context = super(ViewMixin, self).get_context_data(**kwargs)
        context["CACHE_SERVERS"] = settings.CACHE_SERVERS
        return context

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(ViewMixin, self).dispatch(*args, **kwargs)

class DateMixin(object):

    month_format = "%m"
    date_field = 'date'

    def get_alert_groups(self):
        none, qs, dated_items = self.get_dated_items()
        day = dated_items["day"]
        queryset = Alert.objects.default(day)
        tickets = Alert.objects.tickets(day)
        alert_groups = []
        for item in tickets:
            alert_groups.append({"ticket": item, "alerts": queryset.filter(ticket=item["ticket"])})
        return alert_groups

    def get_context_data(self, **kwargs):
        context = super(DateMixin, self).get_context_data(**kwargs)
        context["alert_groups"] = self.get_alert_groups()
        return context

class IndexView(ViewMixin, DateMixin, TodayArchiveView):

    template_name= "index.html"

【讨论】:

  • 今天存档视图被擦除,因为在您的原始示例中,您在视图混合中定义了额外的上下文,而不是在 datemixin 中。只是盲目猜测。
【解决方案2】:

不是您问题的确切答案,但考虑到您的目标似乎是为多个视图添加特定上下文这一事实,context processor 可能是一个不错的解决方案!

【讨论】:

  • 是的,我也遇到过这种情况,但现在我很着迷于让这项工作正常工作。
  • 我明白:)。您是否尝试更改 IndexView 从其他类继承的顺序?
  • 感谢您的建议。我试过了,订单似乎没有什么区别。当 get_context_data 在任何继承 TodayArchiveView 的类上定义时,它会从上下文中消失。即使使用单一继承(没有 mixin),它也会丢失。
【解决方案3】:

问题出现是因为您继承了 2 个类。

context = super(IndexView, self).get_context_data(**kwargs) 将使用来自BaseViewMixin 而不是TodayArchiveView 的方法初始化上下文(超级从左到右遍历基类)--> 来自TodayArchiveView 的上下文变量将丢失。

我认为你可以这样做:

def get_context_data(self, **kwargs):
    context = super(IndexView, self).get_context_data(**kwargs)
    context_day = super(BaseViewMixin, self).get_context_data(**kwargs)
    context = dict(context, **context_day)
    ...

【讨论】:

  • 这让我充满希望,但 context_day 仍然没有返回预期的上下文。即使我输入:context_day = super(TodayArchiveView, self).get_context_data(**kwargs),当 TodayArchiveView 列在 IndexView(BaseViewMixin, TodayArchiveView) 类中时,我仍然无法访问它的上下文。
  • TodayArchiveView 你的课对吗? TodayArchiveView 中是否有 get_context_data 的覆盖?
  • TodayArchiveView 是一个 django 通用视图,它继承了具有 get_context_dataBaseDateListView 并且它不会覆盖它。
猜你喜欢
  • 2012-05-07
  • 2013-04-02
  • 2011-10-10
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
  • 2011-08-06
  • 2010-10-14
  • 2013-06-21
相关资源
最近更新 更多