Python自动化之模板继承和cookie
生活随笔
收集整理的這篇文章主要介紹了
Python自动化之模板继承和cookie
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
request請求頭信息
type(request) //查看類 from django.core.handlers.wsgi import WSGIRequest結果會以字典的形式存在request.environ封裝了用戶所有請求信息
模板繼承
主模板
{% block content %} {<% endblock %}字模板
開頭導入
導入其他標簽
{% include "master.html" %}主模板可以渲染
simple_tag
from django.utils.safestring import mark_safe
register = template.Library()
@register.simple_tag
例1:沒有參數
tianqi.py -------------- def number():return 123主模板 {% load tianqi %} {% number %}例2:有參數
tianqi.py -------------- def number(a1,a2):return 123主模板 {% load tianqi %} {% number a1 a2 %}可以加很多參數,不可以加到if語句里,例如{% if number a1 a2 %}是不允許的
例2:有參數
最多支持2個參數,但是支持{% if "123"|number1:"456" %}
cookie
cookie是客戶端的一個小文件
應用場景:用戶登錄認證
res.set_cookie('username111',u)設置cookie
v = reqeust.COOKIES.get('username111')獲取cookie
關閉瀏覽器cookie失效
參數: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獲取(不是絕對,底層抓包可以獲取到也可以被覆蓋)
加密cookie
obj.set_signed_cookie('username',"kangbazi",salt="asdfasdf") request.get_signed_cookie('username',salt="asdfasdf")jquery cookies
var v = $.cookie('per_page_count', {'path': "/user_list/`"});FBV裝飾器
- 裝飾器FBV:def auth(func):def inner(reqeust,*args,**kwargs):v = reqeust.COOKIES.get('username111')if not v:return redirect('/login/')return func(reqeust, *args,**kwargs)return innerCBV裝飾器
CBV:
from django import views
from django.utils.decorators import method_decorator
第一種寫法
from django import viewsfrom django.utils.decorators import method_decoratorclass Order(views.View):@method_decorator(auth)def get(self,reqeust):v = reqeust.COOKIES.get('username111')return render(reqeust,'index.html',{'current_user': v})@method_decorator(auth) def post(self,reqeust):v = reqeust.COOKIES.get('username111')return render(reqeust,'index.html',{'current_user': v})第二種寫法
from django import viewsfrom django.utils.decorators import method_decoratorclass Order(views.View):@method_decorator(auth)def dispatch(self, request, *args, **kwargs):return super(Order,self).dispatch(request, *args, **kwargs)def get(self,reqeust):v = reqeust.COOKIES.get('username111')return render(reqeust,'index.html',{'current_user': v})def post(self,reqeust):v = reqeust.COOKIES.get('username111')return render(reqeust,'index.html',{'current_user': v})第三種寫法(最簡單 推薦)
from django import viewsfrom django.utils.decorators import method_decorator@method_decorator(auth,name='dispatch')class Order(views.View):def get(self,reqeust):v = reqeust.COOKIES.get('username111')return render(reqeust,'index.html',{'current_user': v})def post(self,reqeust):v = reqeust.COOKIES.get('username111')return render(reqeust,'index.html',{'current_user': v})轉載于:https://www.cnblogs.com/wspblog/p/6243484.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Python自动化之模板继承和cookie的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pythony语法小练习
- 下一篇: python 赋值、表达式