我在views.py中有一个ListView,但是在这个类中,如果用户在登录期间保留的会话中退出,我想检查会话
class part_list_view(ListView):
model = part_list
context_object_name = 'part_list'
template_name = 'part_list.html'
def get_context_data(self, **kwargs):
context = super(part_list_view, self).get_context_data(**kwargs)
context['my_list'] = populate_nav_bar()
return context
答案 0 :(得分:2)
在基于类的视图中的任何位置使用self.request
。在您的情况下self.request.session
def get_context_data(self, **kwargs):
session = self.request.session
# now use session however you want
context = super(part_list_view, self).get_context_data(**kwargs)
context['my_list'] = populate_nav_bar()
return context