View decorators

    See Decorating the class for how to use these decorators withclass-based views.

    The decorators in can be used to restrictaccess to views based on the request method. These decorators will returna django.http.HttpResponseNotAllowed if the conditions are not met.

    • requirehttp_methods(_request_method_list)
    • Decorator to require that a view only accepts particular requestmethods. Usage:

    Note that request methods should be in uppercase.

    • require_GET()
    • Decorator to require that a view only accepts the GET method.

    • Decorator to require that a view only accepts the POST method.

    • require_safe()

    • Decorator to require that a view only accepts the GET and HEAD methods.These methods are commonly considered "safe" because they should not havethe significance of taking an action other than retrieving the requestedresource.

    注解

    Web servers should automatically strip the content of responses to HEADrequests while leaving the headers unchanged, so you may handle HEADrequests exactly like GET requests in your views. Since some software,such as link checkers, rely on HEAD requests, you might preferusing require_safe instead of require_GET.

    • (etag_func=None, last_modified_func=None)
    • etag(etag_func)
    • lastmodified(_last_modified_func)
    • These decorators can be used to generate ETag and Last-Modifiedheaders; see.

    The decorators in django.views.decorators.gzip control contentcompression on a per-view basis.

    • This decorator compresses content if the browser allows gzip compression.It sets the Vary header accordingly, so that caches will base theirstorage on the Accept-Encoding header.

    The decorators in can be used to controlcaching based on specific request headers.

    • varyon_cookie(_func)[源代码]
    • varyon_headers(*headers_)[源代码]
    • The Vary header defines which request headers a cache mechanism should takeinto account when building its cache key.

    See using vary headers.

    The decorators in control server andclient-side caching.

    • cachecontrol(**kwargs_)[源代码]
    • nevercache(_view_func)[源代码]

    • This decorator adds a header to a response to indicate that a page should neverbe cached.