python(Django session cookie )

views

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
            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['is_login']:
        return HttpResponse(request.session['username'])
    else:
         return HttpResponse('gun')

 

views2注销

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
            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',note]:
        return render(request, 'index.html', {'username': request.session['username']})
    else:
         return HttpResponse('gun')


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

 

 

login

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/login/" method="POST">
        <input type="text" name="user" />
        <input type="text" name="pwd" />
        <input type="submit" value="提交" />
</form>
</body>
</html>

注销

发表评论