视图装饰器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/boyun58/article/details/89474709

Django提供了几个可以应用于视图的装饰器,以支持各种HTTP功能。

标题允许使用的HTTP方法

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

条件视图处理

django.views.decorators.http可用于控制特定视图上的缓存行为。

GZip压缩

django.views.decorators.gzip控件内容中的装饰器基于每个视图进行压缩。

Vary

django.views.decorators.vary可用于根据特定请求标头控制缓存。

  • vary_on_cookie(func)
  • vary_on_headers(*标题)
    的Vary报头定义了请求头的高速缓存机制建立其缓存键时应该考虑到。

缓存

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

  • cache_control(** kwargs)
    此装饰器Cache-Control通过向其添加所有关键字参数来修补响应的标头。
  • never_cache(view_func)
    此装饰器向响应添加标头,以指示永远不应缓存页面。Cache-Control: max-age=0, no-cache, no-store, must-revalidate

猜你喜欢

转载自blog.csdn.net/boyun58/article/details/89474709
今日推荐