Django之cookie&session

Django之cookie&session,第1张

概述cookie Cookie 是在 HTTP 协议下,服务器或脚本可以维护客户工作站上信息的一种方式。Cookie 是由 Web 服务器保存在用户浏览器(客户端)上的小文本文件,它可以包含有关用户的信息

<div data-md-url="https://i.cnblogs.com/Editposts.aspx?postID=9962659">
<h1 ID="cookie" >cookie
<blockquote >
<p >cookie 是在 http 协议下,服务器或脚本可以维护客户工作站上信息的一种方式。cookie 是由 Web 服务器保存在用户浏览器(客户端)上的小文本文件,它可以包含有关用户的信息。无论何时用户链接到服务器,Web 站点都可以访问 cookie 信息

 :     :         render(req,,{: })
<span https://m.jb51.cc/tag/Js/" target="_blank" >Js</a>-function" https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #407ee7;"&gt;<span https://m.jb51.cc/tag/Js/" target="_blank" >Js</a>-keyword" https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #6666ea;"&gt;def</span> <span https://m.jb51.cc/tag/Js/" target="_blank" >Js</a>-<a href="https://m.jb51.cc/tag/Title/" target="_blank" >Title</a>" https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #766e6b;"&gt;post</span><span https://m.jb51.cc/tag/Js/" target="_blank" >Js</a>-p<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>ms" https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #df5320;"&gt;(self,**<a href="https://www.jb51.cc/tag/kwargs/" target="_blank" >kwargs</a>)</span>:</span>    user = req.POST.get(<span https://m.jb51.cc/tag/Js/" target="_blank" >Js</a>-string" https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #5ab738;"&gt;'user'</span>)    pwd = req.POST.get(<span https://m.jb51.cc/tag/Js/" target="_blank" >Js</a>-string" https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #5ab738;"&gt;'pwd'</span>)    c = models.<a href="https://m.jb51.cc/tag/admin/" target="_blank" >admin</a>istrator.objects.filter(user<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>=user,password=pwd).count()    <span https://m.jb51.cc/tag/Js/" target="_blank" >Js</a>-keyword" https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #6666ea;"&gt;if</span> c > <span https://m.jb51.cc/tag/Js/" target="_blank" >Js</a>-number" https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #df5320;"&gt;0</span>:        rep = redirect(<span https://m.jb51.cc/tag/Js/" target="_blank" >Js</a>-string" https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #5ab738;"&gt;'/index/'</span>)        newuser = user.encode(<span https://m.jb51.cc/tag/Js/" target="_blank" >Js</a>-string" https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #5ab738;"&gt;'utf-8'</span>).decode(<span https://m.jb51.cc/tag/Js/" target="_blank" >Js</a>-string" https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #5ab738;"&gt;'latin-1'</span>)        rep.set_<a href="https://m.jb51.cc/tag/cookie/" target="_blank" >cookie</a>(<span https://m.jb51.cc/tag/Js/" target="_blank" >Js</a>-string" https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #5ab738;"&gt;'user<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>'</span>,newuser,max_age=<span https://m.jb51.cc/tag/Js/" target="_blank" >Js</a>-number" https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #df5320;"&gt;10</span>)        <span https://m.jb51.cc/tag/Js/" target="_blank" >Js</a>-keyword" https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #6666ea;"&gt;return</span> rep    <span https://m.jb51.cc/tag/Js/" target="_blank" >Js</a>-keyword" https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #6666ea;"&gt;else</span>:        massage = <span https://m.jb51.cc/tag/Js/" target="_blank" >Js</a>-string" https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #5ab738;"&gt;'<a href="https://www.jb51.cc/tag/yonghuming/" target="_blank" >用户名</a>或密码<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" >错误</a>'</span>    <span https://m.jb51.cc/tag/Js/" target="_blank" >Js</a>-keyword" https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #6666ea;"&gt;return</span> render(req,{<span https://m.jb51.cc/tag/Js/" target="_blank" >Js</a>-string" https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #5ab738;"&gt;'massage'</span>: massage})

<span ><span >def <span >index<span >(req):
username=req.cookieS.get(<span >'username')
<span >if username:
newuser=username.encode(<span >'latin-1').decode(<span >'utf-8')
<span >return render(req,<span >"app1/index.HTML",{<span >"name": newuser})
<span >return redirect(<span >"/login")


<p >查看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 =           
总结

以上是内存溢出为你收集整理的Django之cookie&session全部内容,希望文章能够帮你解决Django之cookie&session所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1236547.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存