zoukankan      html  css  js  c++  java
  • django中cookie使用及用户认证装饰器

    1、创建一个app01, 并在settings.py设置

    2、在app01/views.py里面,创建 login、logout、 home等视图

    from django.shortcuts import render, redirect, HttpResponse
    
    # Create your views here.
    
    def login_auth(func):
        def inner( request, *args, **kwargs ):
            if request.COOKIES.get("name"):
                return  func(request, *args , **kwargs)
            else:
                old_path = request.get_full_path()
                return redirect("/login/?next=%s" % old_path)
        return inner
    
    def login(request):
        print( request.get_full_path())
        if request.method == "POST":
            username = request.POST.get("username")
            password = request.POST.get("password")
            if username == "egon"  and password == "123":
                old_path =  request.GET.get("next")
                if old_path:
                    red = redirect(old_path)
                else:
                    red = redirect("/index")
                red.set_cookie("name", "egon")
                return  red
        return render(request, "login.html")
    
    # def index(request):
    #     if request.COOKIES.get("name"):
    #         return HttpResponse("我是index页面,只有登录的才能访问")
    #     path = request.get_full_path()
    #     return redirect("/login?next=%s" % path)
    
    @login_auth
    def index(request):
        return HttpResponse("我是index页面,只有登录的才能访问")
    
    
    # def home(request):
    #     if request.COOKIES.get("name"):
    #         return HttpResponse("我是home页面,只有登录的才能访问")
    #     path = request.get_full_path()
    #     return redirect("/login?next=%s" % path)
    
    @login_auth
    def home(request):
        return HttpResponse("我是home页面,只有登录的才能访问")
    
    
    def logout(request):
        red = render(request,"login.html")
        red.delete_cookie("name")
        return  red

    3、在项目templates下创建模板 login.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
    <form action="" method="POST">
        <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
        <p>用户名:<input type="text" name="username"> </p>
        <p>密 码:<input type="password" name="password"> </p>
        <p>用户名:<input type="submit" value="登录"> </p>
    </form>
    
    </body>
    </html>

    4在工程下urls.py里面配置路由

    from django.contrib import admin
    from django.urls import path
    from app01 import  views
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path("login/", views.login),
        path("index/", views.index),
        path("home/", views.home),
        path("logout/", views.logout)
    ]

    5、登录效果如下

  • 相关阅读:
    SRM 588 D2 L3:GameInDarknessDiv2,DFS
    [置顶] ProDinner体验
    [置顶] 强制访问控制内核模块Smack
    Java小项目--坦克大战(version1.0)
    utf-8-validation
    is-subsequence
    perfect-rectangle
    find-the-difference
    longest-absolute-file-path
    first-unique-character-in-a-string
  • 原文地址:https://www.cnblogs.com/harryTree/p/11857461.html
Copyright © 2011-2022 走看看