Django:cookie

一,Django中操作cookie

  1,获取cookie

request.COOKIES[=RAISE_ERROR,salt=,max_age=None)

  参数:

  •   default: 默认值
  •   salt: 加密盐
  •   max_age: 后台控制过期时间

  2,设置cookie

rep =rep.set_cookie(key,value,...)
rep.set_signed_cookie(key,salt
=<span style="color: #800000">'<span style="color: #800000">加密盐<span style="color: #800000">',...)

参数:

  • key,键
  • value='',值
  • max_age=None,超时时间
  • expires=None,超时时间(IE requires expires,so set it if hasn't been already.)
  • path='/',Cookie生效的路径,/ 表示根路径,特殊的:根路径的cookie可以被任何url的页面访问
  • domain=None,Cookie生效的域名
  • secure=False,https传输
  • httponly=False 只能http协议传输,无法被JavaScript获取(不是绝对,底层抓包可以获取到也可以被覆盖

3,删除cookie

= redirect() rep

二、Django中Session相关方法

request.session[] = 123,123) request.session[</span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 所有 键、值、键值对</span>

<span style="color: #000000"> request.session.keys()
request.session.values()
request.session.items()
request.session.iterkeys()
request.session.itervalues()
request.session.iteritems()

</span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 用户session的随机字符串</span>

<span style="color: #000000"> request.session.session_key

</span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 将所有Session失效日期小于当前日期的数据删除</span>

<span style="color: #000000"> request.session.clear_expired()

</span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 检查 用户session的随机字符串 在数据库中是否</span>
request.session.exists(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;session_key</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)

</span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 删除当前用户的所有Session数据</span>

<span style="color: #000000"> request.session.delete()

request.session.set_expiry(value)
    </span>*<span style="color: #000000"&gt; 如果value是个整数,session会在些秒数后失效。
    </span>*<span style="color: #000000"&gt; 如果value是个datatime或timedelta,session就会在这个时间后失效。
    </span>*<span style="color: #000000"&gt; 如果value是0,用户关闭浏览器session就会失效。
    </span>* 如果value是None,session会依赖全局session失效策略。</pre>

session版登录验证

functools <span style="color: #0000ff">def<span style="color: #000000"> check_login(func):
@wraps(func)
<span style="color: #0000ff">def inner(request,<span style="color: #000000">kwargs):
next_url =<span style="color: #000000"> request.get_full_path()
<span style="color: #0000ff">if request.session.get(<span style="color: #800000">"<span style="color: #800000">user<span style="color: #800000">"<span style="color: #000000">):
<span style="color: #0000ff">return func(request,
<span style="color: #000000">kwargs)
<span style="color: #0000ff">else<span style="color: #000000">:
<span style="color: #0000ff">return redirect(<span style="color: #800000">"<span style="color: #800000">/login/?next={}<span style="color: #800000">"<span style="color: #000000">.format(next_url))
<span style="color: #0000ff">return<span style="color: #000000"> inner

<span style="color: #0000ff">def<span style="color: #000000"> login(request):
<span style="color: #0000ff">if request.method == <span style="color: #800000">"<span style="color: #800000">POST<span style="color: #800000">"<span style="color: #000000">:
user = request.POST.get(<span style="color: #800000">"<span style="color: #800000">user<span style="color: #800000">"<span style="color: #000000">)
pwd = request.POST.get(<span style="color: #800000">"<span style="color: #800000">pwd<span style="color: #800000">"<span style="color: #000000">)

    </span><span style="color: #0000ff"&gt;if</span> user == <span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;alex</span><span style="color: #800000"&gt;"</span> <span style="color: #0000ff"&gt;and</span> pwd == <span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;alex1234</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;:
        </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 设置session</span>
        request.session[<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;user</span><span style="color: #800000"&gt;"</span>] =<span style="color: #000000"&gt; user
        </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 获取跳到登陆页面之前的URL</span>
        next_url = request.GET.get(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;next</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)
        </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 如果有,就跳转回登陆之前的URL</span>
        <span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; next_url:
            </span><span style="color: #0000ff"&gt;return</span><span style="color: #000000"&gt; redirect(next_url)
        </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 否则默认跳转到index页面</span>
        <span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;:
            </span><span style="color: #0000ff"&gt;return</span> redirect(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;/index/</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)
</span><span style="color: #0000ff"&gt;return</span> render(request,<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;login.html</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)

@check_login
<span style="color: #0000ff">def<span style="color: #000000"> logout(request):
<span style="color: #008000">#<span style="color: #008000"> 删除所有当前请求相关的session
<span style="color: #000000"> request.session.delete()
<span style="color: #0000ff">return redirect(<span style="color: #800000">"<span style="color: #800000">/login/<span style="color: #800000">"<span style="color: #000000">)

@check_login
<span style="color: #0000ff">def<span style="color: #000000"> index(request):
current_user = request.session.get(<span style="color: #800000">"<span style="color: #800000">user<span style="color: #800000">"<span style="color: #000000">,None)
<span style="color: #0000ff">return render(request,<span style="color: #800000">"<span style="color: #800000">index.html<span style="color: #800000">",{<span style="color: #800000">"<span style="color: #800000">user<span style="color: #800000">"<span style="color: #000000">: current_user})

Session版登录验证

2,Django中的session配置

Django中默认支持Session,其内部提供了5种类型的Session供开发者使用。

3,CBV中加装饰器

</span><span style="color: #0000ff"&gt;def</span><span style="color: #000000"&gt; get(self,request): </span><span style="color: #800000"&gt;"""</span><span style="color: #800000"&gt; 处理GET请求 </span><span style="color: #800000"&gt;"""</span> <span style="color: #0000ff"&gt;return</span> render(request,<span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;login.html</span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;) </span><span style="color: #0000ff"&gt;def</span><span style="color: #000000"&gt; post(self,request): </span><span style="color: #800000"&gt;"""</span><span style="color: #800000"&gt; 处理POST请求 </span><span style="color: #800000"&gt;"""</span><span style="color: #000000"&gt; user </span>= request.POST.get(<span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;user</span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;) pwd </span>= request.POST.get(<span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;pwd</span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;) </span><span style="color: #0000ff"&gt;if</span> user == <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;alex</span><span style="color: #800000"&gt;'</span> <span style="color: #0000ff"&gt;and</span> pwd == <span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;alex1234</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;: next_url </span>= request.GET.get(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;next</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;) </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 生成随机字符串</span> <span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 写浏览器cookie -> session_id: 随机字符串</span> <span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 写到服务端session:</span> <span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; {</span> <span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; "随机字符串": {'user':'alex'}</span> <span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; }</span> request.session[<span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;user</span><span style="color: #800000"&gt;'</span>] =<span style="color: #000000"&gt; user </span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; next_url: </span><span style="color: #0000ff"&gt;return</span><span style="color: #000000"&gt; redirect(next_url) </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;: </span><span style="color: #0000ff"&gt;return</span> redirect(<span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;/index/</span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;) </span><span style="color: #0000ff"&gt;return</span> render(request,<span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;login.html</span><span style="color: #800000"&gt;'</span>)</pre>

要在CBV视图中使用我们上面的check_login装饰器,有以下三种方式:

from django.utils.decorators import method_decorator

1. 加在CBV视图的get或post方法上

django.utils.decorators <span style="color: #0000ff">class<span style="color: #000000"> HomeView(View):

</span><span style="color: #0000ff"&gt;def</span> dispatch(self,request,**<span style="color: #000000"&gt;kwargs):
    </span><span style="color: #0000ff"&gt;return</span> super(HomeView,self).dispatch(request,**<span style="color: #000000"&gt;kwargs)

</span><span style="color: #0000ff"&gt;def</span><span style="color: #000000"&gt; get(self,request):
    </span><span style="color: #0000ff"&gt;return</span> render(request,<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;home.html</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)

@method_decorator(check_login)
</span><span style="color: #0000ff"&gt;def</span><span style="color: #000000"&gt; post(self,request):
    </span><span style="color: #0000ff"&gt;print</span>(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;Home View POST method...</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)
    </span><span style="color: #0000ff"&gt;return</span> redirect(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;/index/</span><span style="color: #800000"&gt;"</span>)</pre>

2. 加在dispatch方法上

django.utils.decorators <span style="color: #0000ff">class<span style="color: #000000"> HomeView(View):

@method_decorator(check_login)
</span><span style="color: #0000ff"&gt;def</span> dispatch(self,<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;home.html</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)

</span><span style="color: #0000ff"&gt;def</span><span style="color: #000000"&gt; post(self,request):
    </span><span style="color: #0000ff"&gt;print</span>(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;Home View POST method...</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)
    </span><span style="color: #0000ff"&gt;return</span> redirect(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;/index/</span><span style="color: #800000"&gt;"</span>)</pre>

因为CBV中首先执行的就是dispatch方法,所以这么写相当于给get和post方法都加上了登录校验。

3. 直接加在视图类上,但method_decorator必须传 name 关键字参数

如果get方法和post方法都需要登录校验的话就写两个装饰器。

复制代码

from django.utils.decorators import method_decorator

@method_decorator(check_login,name="get")
@method_decorator(check_login,name="post")
class HomeView(View):

def dispatch(self,**kwargs):
    return super(HomeView,**kwargs)

def get(self,request):
    return render(request,"home.html")

def post(self,request):
    print("Home View POST method...")
    return redirect("/index/")</pre>

4,补充

CSRF Token相关装饰器在CBV只能加到dispatch方法上

备注:

复制代码

from django.views.decorators.csrf import csrf_exempt,csrf_protect

class HomeView(View):

@method_decorator(csrf_exempt)
def dispatch(self,request):
    print("Home View POST method...")
    return redirect("/index/")</pre>

三,分页

(self,current_page,total_count,base_url,per_page=10,max_show=11 == 1 self.current_page </span>=<span style="color: #000000"&gt; current_page self.total_count </span>=<span style="color: #000000"&gt; total_count self.base_url </span>=<span style="color: #000000"&gt; base_url self.per_page </span>=<span style="color: #000000"&gt; per_page self.max_show </span>=<span style="color: #000000"&gt; max_show </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 总页码</span> total_page,per_page) </span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; more: total_page </span>+= 1<span style="color: #000000"&gt; half_show </span>= int((max_show - 1) / 2<span style="color: #000000"&gt;) self.half_show </span>=<span style="color: #000000"&gt; half_show self.total_page </span>=<span style="color: #000000"&gt; total_page @property </span><span style="color: #0000ff"&gt;def</span><span style="color: #000000"&gt; start(self): </span><span style="color: #0000ff"&gt;return</span> (self.current_page - 1) *<span style="color: #000000"&gt; self.per_page @property </span><span style="color: #0000ff"&gt;def</span><span style="color: #000000"&gt; end(self): </span><span style="color: #0000ff"&gt;return</span> self.current_page *<span style="color: #000000"&gt; self.per_page </span><span style="color: #0000ff"&gt;def</span><span style="color: #000000"&gt; page_html(self): </span><span style="color: #0000ff"&gt;if</span> self.current_page <=<span style="color: #000000"&gt; self.half_show: show_start </span>= 1<span style="color: #000000"&gt; show_end </span>=<span style="color: #000000"&gt; self.max_show </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;: </span><span style="color: #0000ff"&gt;if</span> self.current_page + self.half_show >=<span style="color: #000000"&gt; self.total_page: show_start </span>= self.total_page -<span style="color: #000000"&gt; self.max_show show_end </span>=<span style="color: #000000"&gt; self.total_page </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;: show_start </span>= self.current_page -<span style="color: #000000"&gt; self.half_show show_end </span>= self.current_page +<span style="color: #000000"&gt; self.half_show </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 生成页面上显示的页码</span> page_html_list =<span style="color: #000000"&gt; [] </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 加首页</span> first_li = <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="{}?page=1"&gt;首页</a></li></span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;.format(self.base_url) page_html_list.append(first_li) </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 加上一页</span> <span style="color: #0000ff"&gt;if</span> self.current_page == 1<span style="color: #000000"&gt;: prev_li </span>= <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="#"&gt;上一页</a></li></span><span style="color: #800000"&gt;'</span> <span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;: prev_li </span>= <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="{0}?page={1}"&gt;上一页</a></li></span><span style="color: #800000"&gt;'</span>.format(self.base_url,self.current_page - 1<span style="color: #000000"&gt;) page_html_list.append(prev_li) </span><span style="color: #0000ff"&gt;for</span> i <span style="color: #0000ff"&gt;in</span> range(show_start,show_end + 1<span style="color: #000000"&gt;): </span><span style="color: #0000ff"&gt;if</span> i ==<span style="color: #000000"&gt; self.current_page: li_tag </span>= <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li class="active"&gt;<a href="{0}?page={1}"&gt;{1}</a></li></span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;.format(self.base_url,i) </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;: li_tag </span>= <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="{0}?page={1}"&gt;{1}</a></li></span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;.format(self.base_url,i) page_html_list.append(li_tag) </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 加下一页</span> <span style="color: #0000ff"&gt;if</span> self.current_page ==<span style="color: #000000"&gt; self.total_page: next_li </span>= <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="#"&gt;下一页</a></li></span><span style="color: #800000"&gt;'</span> <span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;: next_li </span>= <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="{0}?page={1}"&gt;下一页</a></li></span><span style="color: #800000"&gt;'</span>.format(self.base_url,self.current_page + 1<span style="color: #000000"&gt;) page_html_list.append(next_li) </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 加尾页</span> page_end_li = <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="{0}?page={1}"&gt;尾页</a></li></span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;.format(self.base_url,self.total_page) page_html_list.append(page_end_li) </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800000"&gt;""</span>.join(page_html_list)</pre>

使用:

def user_list(request):
    pager = Pagination(request.GET.get("page"),len(data),request.path_info)
    user_list = data[pager.start:pager.end]
    page_html = pager.page_html()
    return render(request,"user_list.html",{"user_list": user_list,"page_html": page_html})

相关文章

注:所有源代码均实测运行过。所有源代码均已上传CSDN,请有...
继承APIView和ViewSetMixin;作用也与APIView基本类似,提供...
一、Django介绍Python下有许多款不同的 Web 框架。Django是重...
本文从nginx快速掌握到使用,gunicorn快速掌握到使用,实现小...
uniapp微信小程序订阅消息发送服务通知
Django终端打印SQL语句 1 Setting配置: 2 默认python 使用的...