【发布时间】: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