如何重新初始化会话变量?

时间:2019-11-20 11:31:04

标签: django session-variables logout

我已经开发了一个Django项目,该项目带有使用Vitor Freitas的教程(https://simpleisbetterthancomplex.com)开发的身份验证应用程序。 现在我想知道当用户注销时如何重新初始化会话变量吗? 目前,它不会重新初始化我的会话变量

我没有开发任何身份验证视图,所以我知道该怎么做

1 个答案:

答案 0 :(得分:0)

感谢您的回复

例如,我有用于用户选择的“选择”变量会话

即使用户注销,最后一个用户的选择仍是存储

@login_required
def index(request):
    # info saisies en dur pour les tests avant implémentation models correspondants
    user_site_typ = 'International'
    user_pays = 'CIV'
    user_site_abb = 'TR'

    request.session['visited'] = False  
    liste_existe = True

    if request.POST:

        # liste déroulante
        selection = request.POST.get('selection', False)
        request.session['selection'] = selection

        if user_site_typ == 'International':
            if selection == 'All patients':
                paginator = Paginator(Preinclusion.objects.all(), 5)
            elif selection =='Randomized':
                paginator = Paginator([patient for patient in Preinclusion.objects.all() if patient.is_randomized], 5)
            else:
                paginator = Paginator([patient for patient in Preinclusion.objects.all() if not patient.is_randomized], 5)

        elif user_site_typ == 'National':
            if selection == 'All patients':
                paginator = Paginator(Preinclusion.objects.filter(pat_num__startswith = user_pays), 5)
            elif selection =='Randomized':
                paginator = Paginator([patient for patient in Preinclusion.objects.filter(pat_num__startswith = user_pays) if patient.is_randomized], 5)
            else:
                paginator = Paginator([patient for patient in Preinclusion.objects.filter(pat_num__startswith = user_pays) if not patient.is_randomized], 5)

        else:
            if selection == 'All patients':
                paginator = Paginator(Preinclusion.objects.filter(Q(pat_num__startswith = user_pays) & Q(pat_num__contains = user_site_abb)), 5)
            elif selection =='Randomized':
                paginator = Paginator([patient for patient in Preinclusion.objects.filter(Q(pat_num__startswith = user_pays) & Q(pat_num__contains = user_site_abb)) if patient.is_randomized], 5)
            else:
                paginator = Paginator([patient for patient in Preinclusion.objects.filter(Q(pat_num__startswith = user_pays) & Q(pat_num__contains = user_site_abb)) if not patient.is_randomized], 5)        

        # Search
        # if ide == "":

    else:
        # récupération du choix de l'utilisateur pour la navigation entre page
        if request.session.get('selection') == '':
            selection = 'All patients'
        else:
            selection = request.session.get('selection')

        if user_site_typ == 'International':
            if selection == 'All patients':
                paginator = Paginator(Preinclusion.objects.all(), 5)
            elif selection =='Randomized':
                paginator = Paginator([patient for patient in Preinclusion.objects.all() if patient.is_randomized], 5)
            else:
                paginator = Paginator([patient for patient in Preinclusion.objects.all() if not patient.is_randomized], 5)

        elif user_site_typ == 'National':
            if selection == 'All patients':
                paginator = Paginator(Preinclusion.objects.filter(pat_num__startswith = user_pays), 5)
            elif selection =='Randomized':
                paginator = Paginator([patient for patient in Preinclusion.objects.filter(pat_num__startswith = user_pays) if patient.is_randomized], 5)
            else:
                paginator = Paginator([patient for patient in Preinclusion.objects.filter(pat_num__startswith = user_pays) if not patient.is_randomized], 5)

        else:
            if selection == 'All patients':
                paginator = Paginator(Preinclusion.objects.filter(Q(pat_num__startswith = user_pays) & Q(pat_num__contains = user_site_abb)), 5)
            elif selection =='Randomized':
                paginator = Paginator([patient for patient in Preinclusion.objects.filter(Q(pat_num__startswith = user_pays) & Q(pat_num__contains = user_site_abb)) if patient.is_randomized], 5)
            else:
                paginator = Paginator([patient for patient in Preinclusion.objects.filter(Q(pat_num__startswith = user_pays) & Q(pat_num__contains = user_site_abb)) if not patient.is_randomized], 5)        

    if paginator.count == 0:
        liste_existe = False

    page = request.GET.get('page')
    try:
        preincluded = paginator.page(page)
    except PageNotAnInteger:
        preincluded = paginator.page(1)
    except EmptyPage:
        preincluded = paginator.page(paginator.num_pages)

    return render(request, 'randomization/index.html', {'preincluded': preincluded, 'liste_existe': liste_existe})