加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Django之cookie&session

发布时间:2020-12-15 17:18:45 所属栏目:大数据 来源:网络整理
导读:div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?postid=9962659"gt; h1 id="cookie" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 24px; border-bottom: 2px solid #a

<div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?postid=9962659"&gt;
<h1 id="cookie" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 24px; border-bottom: 2px solid #aaaaaa;">cookie
<blockquote style="margin: 1.2em 0px; border-left: 4px solid #dddddd; padding: 0px 1em; color: #777777; quotes: none;">
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">Cookie 是在 HTTP 协议下,服务器或脚本可以维护客户工作站上信息的一种方式。Cookie 是由 Web 服务器保存在用户浏览器(客户端)上的小文本文件,它可以包含有关用户的信息。无论何时用户链接到服务器,Web 站点都可以访问 Cookie 信息

 :
     :
         render(req,,{: })
<span class="hljs-function" style="color: #407ee7;"&gt;<span class="hljs-keyword" style="color: #6666ea;"&gt;def</span> <span class="hljs-title" style="color: #766e6b;"&gt;post</span><span class="hljs-params" style="color: #df5320;"&gt;(self,**kwargs)</span>:</span>
    user = req.POST.get(<span class="hljs-string" style="color: #5ab738;"&gt;'user'</span>)
    pwd = req.POST.get(<span class="hljs-string" style="color: #5ab738;"&gt;'pwd'</span>)
    c = models.Administrator.objects.filter(username=user,password=pwd).count()
    <span class="hljs-keyword" style="color: #6666ea;"&gt;if</span> c > <span class="hljs-number" style="color: #df5320;"&gt;0</span>:
        rep = redirect(<span class="hljs-string" style="color: #5ab738;"&gt;'/index/'</span>)
        newuser = user.encode(<span class="hljs-string" style="color: #5ab738;"&gt;'utf-8'</span>).decode(<span class="hljs-string" style="color: #5ab738;"&gt;'latin-1'</span>)
        rep.set_cookie(<span class="hljs-string" style="color: #5ab738;"&gt;'username'</span>,newuser,max_age=<span class="hljs-number" style="color: #df5320;"&gt;10</span>)
        <span class="hljs-keyword" style="color: #6666ea;"&gt;return</span> rep
    <span class="hljs-keyword" style="color: #6666ea;"&gt;else</span>:
        massage = <span class="hljs-string" style="color: #5ab738;"&gt;'用户名或密码错误'</span>
    <span class="hljs-keyword" style="color: #6666ea;"&gt;return</span> render(req,{<span class="hljs-string" style="color: #5ab738;"&gt;'massage'</span>: massage})

<span class="hljs-function" style="color: #407ee7;"><span class="hljs-keyword" style="color: #6666ea;">def <span class="hljs-title" style="color: #766e6b;">index<span class="hljs-params" style="color: #df5320;">(req):
username=req.COOKIES.get(<span class="hljs-string" style="color: #5ab738;">'username')
<span class="hljs-keyword" style="color: #6666ea;">if username:
newuser=username.encode(<span class="hljs-string" style="color: #5ab738;">'latin-1').decode(<span class="hljs-string" style="color: #5ab738;">'utf-8')
<span class="hljs-keyword" style="color: #6666ea;">return render(req,<span class="hljs-string" style="color: #5ab738;">"app1/index.html",{<span class="hljs-string" style="color: #5ab738;">"name": newuser})
<span class="hljs-keyword" style="color: #6666ea;">return redirect(<span class="hljs-string" style="color: #5ab738;">"/login")


<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">查看set_cookie参数

 ,max_age=None,expires=None,path=,domain=None,secure=False,httponly=False,samesite=None):

)

 c > :
            req.session[] = 
            req.session[] = user
             redirect()
        :
            massage = 
         render(req,{: massage})

 :
     :
        is_login = request.session.get()
         is_login:
             func(request,**kwargs)
        :
             redirect()
     inner

   
SESSION_COOKIE_PATH =                   
SESSION_COOKIE_DOMAIN =          
SESSION_COOKIE_SECURE =          
SESSION_COOKIE_HTTPONLY =       
SESSION_COOKIE_AGE =           
SESSION_EXPIRE_AT_BROWSER_CLOSE =        
SESSION_SAVE_EVERY_REQUEST =           

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读