View decorator

This is the 20th day of my participation in the August Text Challenge.More challenges in August

Thank you for meeting you. I’m Y Dazhuang

By Y Dazhuang Link: juejin.cn/user/756923… The copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.

🌊🌈

Part of the article and pictures from the Internet, if you have any questions please contact me

🌊🌈

View decorator

Django provides a number of decorators that support a variety of HTTP features for views.

Look at the decorator classes to see how these decorators can be used in class-based views.

1, require_http_methods

In django. Views. Decorators. Decorators can be used according to the request of the HTTP method to restrict access to view. If conditions are not met, the decorator will return to django. HTTP. HttpResponseNotAllowed.

In practice, Django's built-in view decorator allows you to restrict views to GET or POST requests and not to others. For example, this view can only be accessed through method of GET.Copy the code
  • require_http_methods

    Decorators can require views to accept only specific request methods. Usage:

    from django.views.decorators.http import require_http_methods
    
    @require_http_methods(["GET"."POST"])
    def text_view(request) :
        pass
    Copy the code

    Note that the request method should be uppercase.

  • require_GET() Decorators can require views to accept only GET methods.
  • require_POSTThe decorator can require the view to accept only the POST method
  • The require_Safe () decorator can require a view to accept only GET and HEAD methods. These methods are generally considered secure because they do nothing special other than retrieve the requested resource.

    annotations

    The Web server automatically removes the corresponding content for the HEAD request and leaves the header unchanged, so you can process the HEAD request just as you would a GET request in a view. Because some software relies on HEAD requests (such as link detectors), you need to use require_safe instead of require_GET.

2. Conditional view processing

The following django. Views. Decorators. HTTP decorator is used to control caching behavior in special view.

Decorator functionality to support the view's conditional retrieval (or change). These parameters are callable and are used to calculate the resources requested by the ETag and the last modification time. Callable items are passed the same way as arguments as the view itself. The ETag function should return a string (or "None" if the resource does not exist), while the function last \u modified should return a Datetime object (or None if the resource does not exist). The ETag function should return a complete ETag, including quotesCopy the code
  • condition(etag_func=None.last_modified_func=None)
  • etag(etag_func)
  • last_modified(last_modified_func)

    These decorators are used to generate ETag and Last-Modified headers; Check conditional View Processing.

3. GZip compression

The browser allows gzip compression, and this middleware compresses the content. It sets the Vary header accordingly so that the cache stores it as a base on the receiving encoding header. Error notes bilingual control pinyinCopy the code

Django. Views. Decorators. Gzip) decorator control based on the content of each view in compression.

  • gzip_page()

    If the browser allows gzip compression, this decorator compresses the content. It sets the Vary header accordingly so that the cache will be stored based on the Accept-Encoding header.

4, than the head

Add "Cookie" to the view decorator for the Vary header of the response. This indicates that the content of the page depends on the Cookie. @vary_on_cookie def index(request): passCopy the code

Django. Views. Decorators. Than in the decorator is used to cache control according to the special request header.

  • vary_on_cookie(func)
  • vary_on_headers**headers

    The Vary header defines which request headers the caching mechanism should consider when building its cache key.

    See using Vary Headers.

5, caching,

Django. Views. Decorators. Decorators in the cache control server and the client cache.

  • cache_control( **kwargs)

    This decorator fixes the cache-control header of the response by adding all keyword arguments. See patch_cache_control()) for details of the transformation.

  • never_cache(view_func))

    The decorator adds cache-control: max-age=0, no-cache, no-store, and must-revalidate headers to a response to indicate that caching of the page is prohibited.