Django从Context_Processor函数中访问会话变量

时间:2013-05-10 11:40:57

标签: django

我在视图中设置了一个会话变量:

def festival_theme(request, year, month, day, slug):
    festival = Project.objects.get(category__name=__('festival'), slug=slug)

    request.session['_active_festival_id'] = festival.id

    return render(request, 'web/festival/theme.html', {'festival':festival,})

比我的上下文处理器函数内部我希望获得此会话变量的值。我怎样才能做到这一点?

我试过了:

#context_processors.py
def festivals(request):
    s = SessionStore()
    activeFestivalId = s['_active_festival_id']
    allFestivals = Project.objects.filter(category__name='festival').order_by('-date')
    return {'allFestivals':allFestivals}

1 个答案:

答案 0 :(得分:2)

您应该可以在上下文处理器中使用request.session来访问会话。

#context_processors.py
def festivals(request):
    activeFestivalId = request.session.get('_active_festival_id', None)
    allFestivals = Project.objects.filter(
        category__name='festival').order_by('-date')
    return {'allFestivals': allFestivals}