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、登录效果如下

  • 相关阅读:
    简单的MsChart使用与遇到的麻烦
    SQLServer中case when 与列拼接
    关于集成单点登录问题
    IIS部署网站后,只有本服务器才能登录
    获取本周的周一日期与本周的周日日期
    34个漂亮的应用程序后台管理系统界面(系列二)
    2011年最佳免费 PSD 用户界面素材揭晓
    编程你使用快捷键了吗?
    汉字转全拼音函数优化方案(SQLServer),值得你看看
    WinForm企业应用框架设计【四】动态创建业务窗体
  • 原文地址:https://www.cnblogs.com/harryTree/p/11857461.html
Copyright © 2011-2022 走看看