回到顶部

阅读目录

django require_http_methods、require_GET、require_POST、require_safe

源码路径

..\Lib\site-packages\django\views\decorators\http.py

部分源码

"""
Decorators for views based on HTTP headers.
"""

import logging
from calendar import timegm
from functools import wraps

from django.http import HttpResponseNotAllowed
from django.middleware.http import ConditionalGetMiddleware
from django.utils.cache import get_conditional_response
from django.utils.decorators import available_attrs, decorator_from_middleware
from django.utils.http import http_date, quote_etag

conditional_page = decorator_from_middleware(ConditionalGetMiddleware)

logger = logging.getLogger('django.request')


def require_http_methods(request_method_list):
    """
    Decorator to make a view only accept particular request methods.  Usage::

        @require_http_methods(["GET", "POST"])
        def my_view(request):
            # I can assume now that only GET or POST requests make it this far
            # ...

    Note that request methods should be in uppercase.
    """
    def decorator(func):
        @wraps(func, assigned=available_attrs(func))
        def inner(request, *args, **kwargs):
            if request.method not in request_method_list:
                logger.warning(
                    'Method Not Allowed (%s): %s', request.method, request.path,
                    extra={'status_code': 405, 'request': request}
                )
                return HttpResponseNotAllowed(request_method_list)
            return func(request, *args, **kwargs)
        return inner
    return decorator


require_GET = require_http_methods(["GET"])
require_GET.__doc__ = "Decorator to require that a view only accepts the GET method."

require_POST = require_http_methods(["POST"])
require_POST.__doc__ = "Decorator to require that a view only accepts the POST method."

require_safe = require_http_methods(["GET", "HEAD"])
require_safe.__doc__ = "Decorator to require that a view only accepts safe methods: GET and HEAD."

使用方法

views.py

from django.views.decorators.http import require_GET, require_POST
from django.views.decorators.http import require_http_methods
from django.views.decorators.http import require_safe


@require_GET
def test_get(request):
    """
    Decorator to require that a view only accepts the GET method.
    require_GET 装饰器只接受 GET 请求,否则返回 405 网页状态码。
    """
    pass
    
    
@require_POST
def test_post(request):
    """
    Decorator to require that a view only accepts the POST method.
    require_POST 装饰器只接受 POST 请求,否则返回 405 网页状态码。
    """
    pass
    
    
@require_http_methods(["GET", "POST"])
def test_get_and_post(request):
    """
    Decorator to require that a view only accepts safe methods: GET and POST.
    装饰器接受 GET 和 POST 请求,否则返回 405 网页状态码。
    """
    pass


@require_safe
def test_get_and_head(request):
    """
    Decorator to require that a view only accepts safe methods: GET and HEAD.
    require_safe 装饰器只接受 GET 和 HEAD 请求,否则返回 405 网页状态码。
    """
    pass

 


^_^
请喝咖啡 ×

文章部分资料可能来源于网络,如有侵权请告知删除。谢谢!

前一篇: 英语单词词性的简写意思
下一篇: django 性能监控工具(飞机票)