zoukankan      html  css  js  c++  java
  • cookie实现用户登录验证

    cookie实现用户登录验证

    1,

    INSTALLED_APPS中注册app03

    2,在主程序中新建映射关系到app3的url中
    1 from django.conf.urls import url,include#引入include模块
    2 from django.contrib import admin
    3 
    4 urlpatterns = [
    5     url(r'^admin/', admin.site.urls),
    6     url(r'^a/', include("app1.urls")),
    7     url(r'^b/', include("app2.urls")),
    8     url(r'^c/', include("app3.urls")),
    9 ]

    3.视图函数

    user_info = {
        'asd': {'pwd': "123123"},
        'qwe': {'pwd': "666123"},
    }
    
    
    def login(request):
        if request.method == "GET":
            return render(request, 'test3login.html')  # 用户请求方式为get时,返回test3login.html信息
        if request.method == "POST":  # 如果用户为post请求时,获取用户数据
            u = request.POST.get('username')
            p = request.POST.get('pwd')
            dic = user_info.get(u)
    
            if not dic:
                return render(request, 'test3login.html')
            if dic['pwd'] == p:
                res = redirect('/c/test3/')
                res.set_cookie('username111')
                # res.set_cookie('username111',u,max_age=10)
                # import datetime
                # current_date = datetime.datetime.utcnow()
                # current_date = current_date + datetime.timedelta(seconds=5)
                # res.set_cookie('username111',u,expires=current_date)
                res.set_cookie('username111', u)
                res.set_cookie('user_type', "f", httponly=True)
                return res
            else:
                return render(request, 'test3login.html')
    
    
    def index(reqeust):
        # 获取当前已经登录的用户
        v = reqeust.COOKIES.get('username111')
        
        return render(reqeust,'index.html',{'current_user': v})

     4,test3login.html

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Title</title>
     6 </head>
     7 <body>
     8 <form method="post" action="/c/test2/">
     9     <input type="text" name="username" placeholder="用户名">
    10     <input type="password" name="pwd" placeholder="密码">
    11     <input type="submit">
    12 </form>
    13 </body>
    14 </html>

    5,index.html

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Title</title>
     6 </head>
     7 <body>
     8 <h1>欢迎登录:{{ current_user }}</h1>
     9 </body>
    10 </html>
  • 相关阅读:
    update inner join
    Centos 7安装docker
    使用Let's Encrypt搭建永久免费的HTTPS服务
    upstream timed out (10060: A connection attempt failed because the connected party did not properly respond
    MySQL死锁分析一例
    solr定时更新索引遇到的问题(SolrDataImportProperties Error loading DataImportScheduler properties java.lang.NullPointerException)
    Java中的关键字 transient
    Spring 4 使用Freemarker模板发送邮件&添加附件
    Spring 4 创建REST API
    Spring 4 异常处理
  • 原文地址:https://www.cnblogs.com/cerofang/p/8401672.html
Copyright © 2011-2022 走看看