我写了一个观点:
class ShowNotifications(TemplateView):
context = {}
model = Notification
template_name = "notifications.html"
def get_context_data(self, **kwargs):
context = super(ShowNotifications,self).get_context_data(**kwargs)
context['unseen_notifications'] = Notification.objects.filter(body__user=self.request.user).filter(viewed=False)
context['seen_notifications'] = Notification.objects.filter(body__user=self.request.user).filter(viewed=True)
return context
我已经在模板中显示了它的上下文。我创建了一个通知弹出窗口,用户可以在Facebook中看到该通知,就像登录Facebook一样,他们在登录后可以看到其通知。
我制作了“ notifications.html”,并将其包含在通知导航中。当我单击它时,不显示任何内容。但是,当我通过url调用视图时,url(r'^notifications/', ShowNotifications.as_view(), name='notifin') ,
它会显示通知,但我希望它以弹出方式显示。
我怎样才能做到这一点...?需要帮忙..
听起来您想将某些变量添加到每个视图的上下文中,而不仅仅是此视图。
一种方法是使用上下文处理器:
# myapp/context_processors.py
def notifications(request):
"Context processor for adding notifications to the context."
return {
'unseen_notifications': Notification.objects.filter(
body__user=request.user).filter(viewed=False),
'seen_notifications': Notification.objects.filter(
body__user=request.user).filter(viewed=True),
}
您还需要将上下文处理器添加到TEMPLATE_CONTEXT_PROCESSORS:
# settings.py
...
TEMPLATE_CONTEXT_PROCESSORS = (
...
'myapp.context_processors.notifications',
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句