• Django-基于session的登录


    基于session写一个登录认证装饰器,四个视图(登录,退出,订单,用户信息),使用cbv

    一、路由

        path('', views.Login.as_view(),name='login'),
        path('out/', views.Out.as_view()),
        path('userinfo/', views.Userinfo.as_view()),
        path('order/', views.Order.as_view()),
    

    二、视图

    from django.shortcuts import render, HttpResponse, redirect,reverse
    from django.views import View
    from django.utils.decorators import method_decorator
    
    
    def login_auth(func):
        def inner(request, *args, **kwargs):
            session = request.session.get('name')
            if session:
                res = func(request, *args, **kwargs)
                return res
            else:
                path = request.get_full_path()
                return redirect(reverse('login')+'?returnUrl=%s' %path)
        return inner
    
    
    class Login(View):
        def get(self,request):
            return render(request, 'login.html')
    
        def post(self,request):
            username = request.POST.get("username")
            passwd = request.POST.get("passwd")
            if username == 'yang' and passwd == '123':
                returnUrl = request.GET.get("returnUrl")
                request.session["name"] = 'yang'
                if returnUrl:
                    return redirect(returnUrl)
                else:
                    return redirect('/userinfo/')
            else:
                return HttpResponse('账号或密码错误')
    
    
    class Out(View):
        def get(self,request):
            return render(request, 'out.html')
    
    
    @method_decorator(login_auth,name='get')
    class Userinfo(View):
        def get(self, request):
            return render(request, 'userinfo.html')
    
        def post(self, request):
            del self.request.session['name']
            return render(request, 'out.html')
    
    
    @method_decorator(login_auth,name='get')
    class Order(View):
        def get(self, request):
            return render(request, 'order.html')
    
        def post(self, request):
            del self.request.session['name']
            return render(request, 'out.html')
    

    三、模板

    1.login

    <div>
        <form action="" method="post">
           <p>用户名<input type="text" name="username"></p>
           <p>密码<input type="password" name="passwd"></p>
            <input type="submit">
        </form>
    </div>
    

    2.order

    <h1>订单信息</h1>
    <form action="" method="post">
        <input type="submit" value="退出">
    </form>
    

    3.userinfo

    <h1>用户信息</h1>
    <form action="" method="post">
        <input type="submit" value="退出">
    </form>
    

    4.out

    <h1>退出成功</h1>
    
  • 相关阅读:
    redis集群规范
    mongodb的基本使用
    redis进阶
    redis基本使用
    selenium的基本使用
    C++入门
    C语言入门
    MATLAB中矩阵reshape的顺序规律
    Tensorflow:ImportError: DLL load failed: 找不到指定的模块 Failed to load the native TensorFlow runtime
    差分定位和精密定位
  • 原文地址:https://www.cnblogs.com/chiyun/p/14066543.html
Copyright © 2020-2023  润新知