Django Views Decorator
Django的试图函数的装饰器主要有:
HTTP请求方法
该装饰器是设置允许访问HTTP协议的方法,装饰器在django.views.decorators.http
中,默认是允许所有方式,如果添加限制允许访问的方法,则需要给视图函数加上装饰器require_http_methods(request_method_list)
,request_method_list
中定义允许访问的方法。请求方式需要大写
from django.http import HttpResponse, HttpResponseNotFound
from django.views.decorators.http import require_http_methods
@require_http_methods(["POST"])
def my_views(request):
foo = True
if foo:
return HttpResponseNotFound('<h1>Page not found</h1>')
else:
return HttpResponse('<h1>Page was found</h1>')
require_GET()
该装饰器函数仅允许GET方法。
require_POST()
该装饰器函数仅允许POST方法。
require_safe()
该装饰器函数仅允许GET和HEAD方法。
条件视图处理
GZip压缩
改变页眉
缓存
作者: 咕咚!
出处: https://www.cnblogs.com/linga/
关于作者:专注虚拟化,运维开发,RPA,Rust,Go,Python!
本文版权归作者和博客园共有,禁止*.csdn.net转载,禁止以盈利为目的的转载,转载文章,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可邮件(oldsixa@163.com)咨询.