简体   繁体   中英

Django Accessing Session Variable from within Context_Processor function

I am setting a session variable inside my view as:

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,})

than inside my context processor function i want to get this session variable's value. How can i achieve this?

I have tried:

#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}

You should be able to access the session by using request.session in your context processor.

#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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM