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方法。