Django提供了几个可以应用于视图以支持各种HTTP特性的装饰器

Allowed HTTP

django.views.decorators.http里的装饰器可以根据请求方法限制对视图的访问。

require_http_methods

接收特定的HTPP 请求方法

from django.views.decorators.http import require_http_methods

@require_http_methods(["GET", "POST"])
def my_view(request):
# I can assume now that only GET or POST requests make it this far
# ...
pass

注意,请求方法应该是大写的

require_GET()

接收GET()请求

require_POST()

接收POST()请求

require_safe()

接收GET()和HEAD()请求

Conditional view processing

以下django.view .decorators.http中的decorator可用于控制特定视图上的缓存行为。

condition(etag_func=None, last_modified_func=None)

etag(etag_func)

last_modified(last_modified_func)

def latest_entry(request, blog_id):
return Entry.objects.filter(blog=blog_id).latest("published").published from django.views.decorators.http import condition @condition(last_modified_func=latest_entry)
def front_page(request, blog_id):
...

GZip compression

django.view .decorators.gzip中的decorator在每个视图的基础上控制了内容压缩。

gzip_page()

如果浏览器允许gzip压缩,则此装饰器将压缩内容。它相应地设置了Vary标头,以便缓存将其存储基于accept编码标头。

Vary headers

vary中的decorator可用于根据特定的请求头控制缓存。

vary_on_cookie(func)

vary_on_headers(*headers)

不同的标头定义了缓存机制在构建缓存键时应该考虑哪些请求标头。

Caching

django.views.decorators.cache 控制服务端和客户端缓存中的decorator。

cache_control(**kwargs)

这个装饰器通过添加所有关键字参数来修复响应的Cache-Control报头。

never_cache(view_func)

这个修饰符向响应添加了Cache-Control: max-age=0、no-cache、no-store、must-revalidate头,以指示永远不应该缓存页面。

django view 装饰器的更多相关文章

  1. Django - CBV装饰器实现用户登录验证

    一.使用Django自带的decorator 通常情况,使用 函数定义的view,可以直接使用 login_required 直接装饰 @login_required def index(reques ...

  2. python django 自定义 装饰器

    # -*-coding:utf-8-*- __author__ = "GILANG (pleasurelong@foxmail.com)" """ d ...

  3. django添加装饰器

    引入模块: from django.utils.decorators import method_decorator 添加:@method_decorator(func) from django.ut ...

  4. django CBV装饰器 自定义django中间件 csrf跨站请求伪造 auth认证模块

    CBV加装饰器 第一种 @method_decorator(装饰器) 加在get上 第二种 @method_decorator(login_auth,name='get') 加在类上 第三种 @met ...

  5. Django CBV装饰器 中间件 auth模块 CSRF跨站请求

    CBV添加装饰器 给CBV添加装饰器有三种方法,三种方法都需要导入模块: from django.utils.decorators import method_decorator 第一种直接在方法上面 ...

  6. django 使用装饰器验证用户登陆

    使用装饰器验证用户登陆,需要使用@method_decorator 首先需引用,method_decorator,并定义一个闭包 from django.utils.decorators import ...

  7. Django自定义装饰器

    装饰器模板: def decorator(func): def wrapper(*args,**kwargs): return func(*args,**kwargs) return wrapper ...

  8. Django 之装饰器实现登录认证

    def check_login(func): # 自定义登录验证装饰器 def warpper(request, *args, **kwargs): is_login = request.sessio ...

  9. 利用Django和装饰器做一个简单的修改密码页面

    view视图代码: from django.shortcuts import render,redirect from django.http import HttpResponse from PIL ...

随机推荐

  1. AngularJS与服务器交互(4)

    转自:http://itindex.net/detail/50919-angularjs-%E6%9C%8D%E5%8A%A1%E5%99%A8-%E4%BA%A4%E4%BA%92 对于AJAX应用 ...

  2. Mysql事务及行级锁

    事务隔离级别 数据库事务隔离级别,只是针对一个事务能不能读取其它事务的中间结果. Read Uncommitted (读取未提交内容) 在该隔离级别,所有事务都可以看到其他未提交事务的执行结果.本隔离 ...

  3. svn: Can't connect to host

    关于“svn: Can't connect to host '*.*.*.*': 由于连接方在一段时间后没有正确答复或连接”的解决方法   阿里云服务器环境(PHP+Nginx+MySQL) [原因1 ...

  4. 相机IMU融合四部曲(二):误差状态四元数详细解读

    相机IMU融合四部曲(二):误差状态四元数详细解读 极品巧克力 前言 上一篇文章,<D-LG-EKF详细解读>中,讲了理论上的SE3上相机和IMU融合的思想.但是,还没有涉及到实际的操作, ...

  5. MongoDB 那些坑

    MongoDB 是目前炙手可热的 NoSQL 文档型数据库,它提供的一些特性很棒:如自动 failover 机制,自动 sharding,无模式 schemaless,大部分情况下性能也很棒.但是薄荷 ...

  6. IOS操作系统上执行monkey测试

    IOS操作系统上执行monkey测试 IOS操作系统不像Android系统那么方便,各种限制也比较多,目前我的建议还是直接在模拟器上执行monkey测试.如果需要在真机上面执行,可以参考文档: htt ...

  7. web.xml配置详解[转]

    引文: 对于一个J2EE领域的程序员而言,基本上每天都会和web应用打交道. 什么是web应用?最简单的web应用什么样?给你一个web应用你该从何入手? 1.什么是web应用? web应用是一种可以 ...

  8. [C++] const and char*

    const and char* NOTICE:   char  *str = "hello";  the value of str is the address of the fi ...

  9. AutoComplete的extraParams动态传递参数

    AutoComplete可利用extraParams传递参数,如 extraParams:{para1:'参数1',para2:'参数2'} 但是,如需动态取值作为参数值时却无法达到期望目的,可改为配 ...

  10. java重载方法的二义性

    http://blog.csdn.net/lavor_zl/article/details/40746813