Python自动化之模板继承和cookie
request请求头信息
type(request) //查看类
from django.core.handlers.wsgi import WSGIRequest结果会以字典的形式存在
request.environ封装了用户所有请求信息
模板继承
主模板
{% block content %}
{<% endblock %}
字模板
开头导入
{% extends "master.html" %}{% block content %}内容{% endblock %}
导入其他标签
{% include "master.html" %}
主模板可以渲染
simple_tag
- 在app下新建一个templatetags目录,建一个py文件.
- from django import template
from django.utils.safestring import mark_safe
register = template.Library()
@register.simple_tag - 在主模板添加{% load py文件名%}称 ,{% 函数名称 %}}
例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:有参数
@register.filter
def number1(a1,a2):return 123{{ "123"|number1:"456" }}
最多支持2个参数,但是支持{% if "123"|number1:"456" %}
cookie
cookie是客户端的一个小文件
应用场景:用户登录认证
def login(request):if request.method == "GET":return render(request,'login.html')if request.method == "POST":u = request.POST.get('username')p = request.POST.get('pwd')dic = user_info.get(u)if not dic:return render(request,'login.html')if dic['pwd'] == p:res = redirect('/index/')# 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',"asdfjalskdjf",httponly=True)return reselse:return render(request,'login.html')def index(reqeust):
# 获取当前已经登录的用户v = reqeust.COOKIES.get('username111')return render(reqeust,'index.html',{'current_user': v})
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 inner
CBV装饰器
CBV:
from django import views
from django.utils.decorators import method_decorator
@method_decorator(auth,name='dispatch')class Order(views.View):# @method_decorator(auth)# def dispatch(self, request, *args, **kwargs):# return super(Order,self).dispatch(request, *args, **kwargs)# @method_decorator(auth)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_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
Python自动化之模板继承和cookie相关推荐
- python自动化接口测试中的cookies怎么实现_Python接口自动化之cookie、session应用
------·今天距2021年260天·------ 这是ITester软件测试小栈第112次推文 在上一篇Python接口自动化测试系列文章:Python接口自动化-requests模块之post请 ...
- python 自动化识别H5模板与UI设计是否一致
公司开发根据UI设计制作了2000多个H5网页.利用python 自动化去挑选2000多个模板中错误的模板.经过粗略的查看,发现有些模板显示为空白,或者与原UI设计不一致,根据这个来设计方案.首先考虑 ...
- python自动化高效办公第二期,带你项目实战【一】{excel数据处理、批量化生成word模板、pdf和ppt等自动化操作}
相关文章和数据源: Python自动化办公--Pandas玩转Excel[一] Python自动化办公--Pandas玩转Excel数据分析[二] Python自动化办公--Pandas玩转Excel ...
- itext word模板替换_【极简Python 自动化办公】Python写入Word文档
[极简Python 自动化办公]专栏是介绍如何利用python办公,减少工作负荷.篇幅精炼,内容易懂,无论是否有编程基础,都非常适合. 在上次文章中,我们学习了[用python写入excel],这次我 ...
- Python框架篇之Django(Template模版:标签tag、自定义filter、extend模板继承)
文章目录 一.标签的使用(tag) 二.自定义filter和simple_tag 三.extend模板继承 一.标签的使用(tag) 1.语法格式:{% tags %} 2.提供的几种标签 {% if ...
- NodeJS_08_art-template子模板与模板继承_MongoBooster_express-session_三大类中间件_全局错误处理
NodeJS七天课程学习笔记_第8天 Blog综合案例 Blog 综合案例 (包含注册.登录.修改密码.注销.发布.分页列表.评论.个人中心.上传头像等) 课程内容概要: 1. 介绍art-templ ...
- 测试技能整理-python自动化
pycharm工具基本使用及python基本 python简介 Python是一种跨平台.简单易学.面向对象的编程语言一门计算机语言 自动化测试-通过代码取代手工测试 市场自动化语言:python . ...
- Python自动化运维---学习笔记
Python自动化运维 python简介 入门容易精通难,python适合作为第二编程语言(对于运维:Shell,对于开发:Java,对于全栈:nodejs(javascript)) python官网 ...
- Python自动化实践
*** Python自动化实践 *** 1.为什么要写代码实现接口自动化 大家知道很多接口测试工具可以实现对接口的测试,如postman.jmeter.fiddler等等,而且使用方便,那么为什么还要 ...
最新文章
- java取服务器时间的函数,非常有用的Java日期时间操作函数代码一览
- 008_JavaScript输出
- 关于sizeof表达式作为数组元素个数的编译
- python flask web开发 可视化开发_python web开发之——Flask入门教程
- URI和URL及URN的区别
- 巨蟒python全栈开发-第6天 is==
- java ip归属地查询_JAVA版IP地址查询调用示例
- 27、ArrayList和LinkedList的区别
- [特约· Keso ·东拉西扯] 我眼中的乔布斯
- c语言会生成class文件,一文带你刨析class文件
- SNMPv3对安全威胁的分类
- Hive 不支持 where 子句中的子查询, SQL 常用的 exist in 子句需要改写。这一改写相对简单。考虑以下 SQL 查询语句:
- Unity-创建一个小地图
- 3维图像处理的新星--Open3D(实操过程持续更新ing....
- 提升NexusPHP解析种子文件性能(优化benc.php)
- 远控免杀专题文章(4)-Evasion模块免杀(VT免杀率12/71)
- 黑洞同样释放能量--霍金修正黑洞理论
- 手机端页面 自适应解决方案-收集
- PL/SQL存储过程
- Oracle如何实现跨数据库查询
热门文章
- 面向对象(下)知识点
- guido正式发布python年份_Python 基础学习笔记.docx
- 网页游戏怎么修改数据_一周网页游戏数据报告(7.14-7.20)
- icem密度盒怎么设置_seo中关键词密度的问题
- 云栖掠影|回首开源十年,RocketMQ 焕发新生
- 理解 Pod 和容器设计模式
- Serverless 在大规模数据处理的实践
- 普通java跑到linux上,JAVA在linux下open too many files
- mysql重装要删注册表_Mysql重装问题—删除注册表
- oracle bi publisher 安装,Oracle BI Publisher 企业版安装后的配置(BI Publisher Enterprise Edition)...