28_django restriction request method decorators

Method restriction request Django

There are common request: GET / POST

  1. GET: GET requests generally used to obtain data to the server, but will not submit data to the server, no changes to the status of the server
  2. POST: POST request is typically used to submit data to the server, the server's status will be changed

Restriction request method in Django decorator

Django built-in view of the decorator can provide some restrictions to the view. For example, this view can only access the GET method, etc. through.

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

1. reruire_http_methods

from django.http.decorators.http import require_http_methods

@require_http_methods(['GET'])      # require_http_methods(['GET', 'POST'])
def img_captcha(request, img_uuid):
    """
    生成图片验证码
    url: /img_captcha/
    :param request:
    :return:
    """
    pass

2. require_GET

@require_GET
def check_username(request, username):
    """
    校验用户名是否存在
    url: ^/username/(?P<username>\w{3,20})/$
    :param request:
    :return:
    """
    pass

3. require_POST

@require_POST
def send_sms_captcha(request):
    """
    发送短信验证码
    url: /sms_captcha/
    method:  POST
    :param request:
    :return:
    """
    pass

Guess you like

Origin www.cnblogs.com/nichengshishaonian/p/11583898.html