python(Django session cookie设置超时时间选择x’z)

 

from django.shortcuts import render, redirect, HttpResponse


# Create your views here.


def login(request):
    # from django.conf import settings
    # print(settings.CSRF_HEADER_NAME)
    # HTTP_X_CSRFTOKEN
    # X-CSRFtoken

    if request.method == "GET":
        return render(request,'login.html')
    elif request.method == "POST":
        user = request.POST.get('user')
        pwd = request.POST.get('pwd')
        if user == 'root' and pwd == "123":
            # session中设置值
            request.session['username'] = user
            request.session['is_login'] = True
            #设置session超时时间
            if request.POST.get('rmb',None) == '1':
                request.session.set_expiry(10)
            if request.POST.get('rmb',None) == '1':
                # 超时时间
                request.session.set_expiry(10)
            return redirect('/index/')
        else:
            return render(request,'login.html')


def index(request):
    if request.session.get('is_login', None):
        return render(request, 'index.html', {'username': request.session['username']})
    else:
         return HttpResponse('gun')


def logout(request):
    request.session.clear()
    return redirect('/login/')

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>欢迎登陆:{{ username }},</h1>
    <a href="/logout/">注销</a>
</body>
</html>

发表评论