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. .NET WebAPI 实现图片上传(包括附带参数上传图片)

    博主的项目,客户端是APP,考虑到以后也可能会应用到微信端.网站等,图片上传方法就需要兼容多端,并且以目前的设计,不允许非登录用户上传图片,就得在上传时解决附带参数上传图片的问题. 先来看看后台方法( ...

  2. 俄罗斯方块C#版

    using System; using System.Windows.Forms; using System.Drawing; using System.Media; class me : Form ...

  3. Junit的使用

    Junit是用于编写单元测试的框架.对于已经写好的函数,可以使用Junit生成单元测试代码. 自己的环境是:Linux Java环境是:JDK1.7 IDE:Eclipse Java EE IDE f ...

  4. Javascript两个感叹号的用法(!!)

    var foo; alert(!foo);//undefind情况下或者null,一个感叹号返回的是true; alert(!goo);//undefind情况下,一个感叹号返回的也是true; 但是 ...

  5. 源码编译安装postgresql

    依赖的库:libreadline-dev,zlib1g-dev 安装:下载解压源码包,然后 ./configure,make,make install即可.. 注意不能在root账户下跑server, ...

  6. linux安装Jenkins

    一.下载jenkins 最新地址在:https://jenkins.io 我下载的是:Jenkins 2.35.war,下载好直接放到tomcat的webapp目录里,启动tomcat就可以运行了 二 ...

  7. 数据结构作业——expectation(树形dp+dfs)

    expectation Description 给出一棵带权值的树,我们假设从某个节点出发,到目标节点的时间为两个节点之间的最短路.由于出发节点不好选取,所以选在每个节点都有一定的概率,现在我们要求从 ...

  8. CSS样式基础总结

    首行缩进:text-indent:2em 行高:line-height:1.5em 1.5倍行距 也可以设置像素文字修饰:text-decoration:underline下划线 line-throu ...

  9. 换肤系统(oocss方式)

    近期想做一个换肤系统,参考过Bootstrap系统,思前想后,内容不难,但就是理不清楚,主要是换肤系统的css如何设计,怎样设计可重用性最好,后期更方便修改和维护,还有一个最头疼的就是怎么给css进行 ...

  10. c# 面向方面编程

    AOP面向切面编程(Aspect Oriented Programming),是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.Spring框架用的核心技术就是AOP,是函数式编程的一 ...