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

  1. 在app下新建一个templatetags目录,建一个py文件.
  2. from django import template

    from django.utils.safestring import mark_safe

    register = template.Library()

    @register.simple_tag
  3. 在主模板添加{% 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 res
else:
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 views
from django.utils.decorators import method_decorator class 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 views
from django.utils.decorators import method_decorator
class 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 views
from 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})

Python自动化之模板继承和cookie的更多相关文章

  1. Python学习---django模板继承180123

    django模板继承  --20180123 a.include 模板标签 b.extend(继承)模板标签 ------include 模板标签 该标签允许在(模板中)包含其它的模板的内容. 标签的 ...

  2. Selenium3+python自动化013-操作浏览器的Cookie

    为什么要用Cookie?在测试多个页面时候可绕过验证码输入,直接添加cookie,也可以在添加唯一标识时候使用. 一.操作浏览器的Cookie 1.1.验证码的处理方式 说明:WebDriver类库中 ...

  3. python【第二十一篇】Django模板继承、分页、cookie验证

    1.模板继承 母版master.html {% block title %}{% endblock %}2 {% block table-cont %}{% endblock %} 子板 {% ext ...

  4. python测试开发django-7.django模板继承(block和extends)

    前言 打开一个网站时候,点导航栏切换到不同的页面,发现导航部分是不变的,只是页面的主体内容变了,于是就可以写个母模板,其它的子页面继承母模板就可以了. 母模板 可以在母模板中添加多个块标签,每个块标签 ...

  5. appium+python自动化50-生成定位对象模板templet(jinja2)

    前言 每次自己写pageobject定位元素对象太繁琐,格式都差不多,只是换个定位方法,这种就可以才有模板的方式,批量生成pageobject定位元素对象的模板 python里面生成模板有两个模块可以 ...

  6. python 全栈开发,Day70(模板自定义标签和过滤器,模板继承 (extend),Django的模型层-ORM简介)

    昨日内容回顾 视图函数: request对象 request.path 请求路径 request.GET GET请求数据 QueryDict {} request.POST POST请求数据 Quer ...

  7. [Python自学] day-21 (1) (请求信息、html模板继承与导入、自定义模板函数、自定义分页)

    一.路由映射的参数 1.映射的一般使用 在app/urls.py中,我们定义URL与视图函数之间的映射: from django.contrib import admin from django.ur ...

  8. 39.Python模板结构优化-引入模板include标签、模板继承使用详解

    在进行模板的构造时,不免有些模板的部分样式会相同,如果每一个模板都是重写代码的话,不仅在做的时候麻烦,而且在后期的维护上,也是相当的麻烦.所以我们可以将模板结构进行优化,优化可以通过:引入模板:模板继 ...

  9. Python自动化运维之31、Tornado框架

    Tornado 官网:http://www.tornadoweb.org/en/stable/ Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本. ...

随机推荐

  1. BootStrap学习笔记,优缺点总结

    本篇约定Bootstrap简写为BT   BT的受欢迎程度是大家有目共睹的,用它可以快速的搭建出网站.很早就接触过这个框架,其中的栅格系统,css模块化以及js插件做的相当不错,由于工作中较少使用也一 ...

  2. Binding笔记

    Binding基础  绑定某个对象的属性值到控制上,写法如下: public class Order : INotifyPropertyChanged//只要实现此接口 { public event ...

  3. AngularJS指令

    1. AngularJS指令的特点: AngularJS通过被称为指令的新属性来扩展HTML,指令的前缀为ng-. AngularJS通过内置的指令来为应用添加功能. AngularJS允许你自定义指 ...

  4. openvpn 启动

    安装 yum -y install openvpn 配置文件可以放在: /etc/openvpn 例如,我这里的路径: [mslagee@centos-dev ~]$ cd /etc/openvpn/ ...

  5. 【poj3071】 Football

    http://poj.org/problem?id=3071 (题目链接) 题意 ${2^n}$个队伍打淘汰赛,输的被淘汰.第1个队打第2个队,第3个队打第4个队······给出第i个队伍打赢第j个队 ...

  6. bzoj 1070 [SCOI2007]修车

    最小费用最大流. 将每个技术人员拆成车数个点,技术人员i的第j个点代表技术人员i修的倒数第j辆车. 源点向所有技术人员点连一条容量为1费用为0的边. 所有技术人员点向所有车点连边:技术人员i的第j个点 ...

  7. MapControl图层删除或添加触发监听事件

    监听MapControl中对于图层添加和删除的事件 IActiveViewEvents_Event m_MapActiveViewEvents = m_mapControl.Map as IActiv ...

  8. qrcode 生成验证码带文字

    /** * 生成二维码 * * @param int $id * @param string $file * @param boolean $is_download */public function ...

  9. 【转】一千行MySQL学习笔记

    /* 启动MySQL */ net start mysql   /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码   /* 跳过权限验证登录MySQL */ ...

  10. Nodejs事件引擎libuv源码剖析之:句柄(handle)结构的设计剖析

    声明:本文为原创博文,转载请注明出处. 句柄(handle)代表一种对持有资源的索引,句柄的叫法在window上较多,在unix/linux等系统上大多称之为描述符,为了抽象不同平台的差异,libuv ...