zoukankan      html  css  js  c++  java
  • Django-基于Cookie的登录

    基础版

    写2个路由和视图函数,一个是登录,写入cookie,访问order,必须登录以后才能访问,否则重定向到登录页面,order页面实现退出功能,清除cookie,利用登录认证装饰器,必须登录之后获取到了cookie才能进入order函数

    模板文件:login页面三个input标签,分别为用户名,密码,提交按钮。out页面只有一个退出按钮。代码略

    def login_auth(func):
        def inner(request,*args,**kwargs):
            cookies = request.COOKIES.get('username')
            if cookies:
                res = func(request,*args,**kwargs)
                return res
            else:
                return redirect('/login/')
        return inner
    
    
    def login(request):
        if request.method == 'GET':
            return render(request,'login.html')
        else:
            username = request.POST.get("username")
            passwd = request.POST.get("passwd")
            if username == 'yang' and passwd == '123':
                obj = redirect("/out/")
                obj.set_cookie("username",'yang')
                return obj
            else:
                return HttpResponse('账号或密码错误')
    
    @login_auth
    def out(request):
        if request.method == 'GET':
            # cookies = request.COOKIES.get('username')
            # if cookies:
            return render(request,'out.html')
            # else:
            #     return redirect('/login/')
        else:
            obj = HttpResponse('退出成功')
            obj.delete_cookie('username')
            return obj
    

    改进版:

    实现每个网站都需要登录,且登录后直接返回原页面
    cookie版登陆校验

    路由

    # cookie版登录
    path('login/', views.login),
    path('order/', views.order),
    path('logout/', views.logout),
    path('userinfo/', views.userinfo),
    

    视图函数

    # 登录认证装饰器
    def login_auth(func):
        def inner(request, *args, **kwargs):
            # 登录校验
            name = request.COOKIES.get('name')
            if name:
                res = func(request, *args, **kwargs)
                return res
            else:
                path = request.get_full_path()
                return redirect('/login/?returnUrl=%s' % path)
    
        return inner
    
    
    ### cookie版登录
    def login(request):
        if request.method == 'GET':
    
            return render(request, 'login.html')
        else:
            name = request.POST.get('name')
            password = request.POST.get('password')
            if name == 'lqz' and password == '123':
                # 写入cookie
                # 登录成功,重定向
                path = request.GET.get('returnUrl')
                if path:
                    obj = redirect(path)
                else:
                    obj = redirect('/index/')
                obj.set_cookie('name', name)
                return obj
            else:
                return HttpResponse('用户名或密码错误')
    
    @login_auth
    def order(request):
        return render(request, 'order.html')
    
    
    @login_auth
    def userinfo(request):
        return render(request, 'userinfo.html')
    
    
    def logout(request):
        obj = HttpResponse('退出登录成功')
        obj.delete_cookie('name')
        return obj
    

    模板

    login.html

    <form action="" method="post">
        <p>用户名:<input type="text" name="name"></p>
        <p>密码:<input type="password" name="password"></p>
        <p><input type="submit" value="提交"></p>
    </form>
    

    order

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <a href="/logout/">点我退出</a>
    </body>
    </html>
    

    userinfo.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h1>用户信息页面</h1>
    </body>
    </html>
    

  • 相关阅读:
    博客图片上传picgo工具安装配置github图传使用
    安装配置hexo icarus主题配置
    通往远方的道路总是漫长的
    java 关于值引用、地址引用的问题
    Java设计模式の迭代器模式
    Java设计模式の模版方法模式
    vim 常用快捷键(整理版)
    Java设计模式の责任链模式
    Java设计模式の代理模式
    java rmi远程方法调用实例
  • 原文地址:https://www.cnblogs.com/chiyun/p/14066548.html
Copyright © 2011-2022 走看看