Django limit request method

Commonly used request methods:
GET request: GET requests are generally used to request data from the server, but will not submit data to the server and will not change the state of the server. For example, get the details of an article from the server.
POST request: POST request is generally used to submit data to the server, which will change the state of the server. For example, submit an article to the server.
Restriction request decorators:
Django's built-in view decorators can provide some restrictions on views. For example, this view can only be accessed through the GET method. Some commonly used built-in view decorators are described below.

django.http.decorators.http.require_http_methods: This decorator requires a list of methods that are allowed to be passed. For example, it can only be accessed through GET. Then the sample code is as follows:

from django.views.decorators.http import require_http_methods

@require_http_methods([“GET”])
def my_view(request):
pass
django.views.decorators.http.require_GET: This decorator is equivalent to a shorthand form of require_http_methods(['GET']), only allowed to use GET method to Access view. The sample code is as follows:

from django.views.decorators.http import require_GET

@require_GET
def my_view(request):
pass
django.views.decorators.http.require_POST: This decorator is equivalent to a shorthand form of require_http_methods(['POST']), which only allows the POST method to access the view. The sample code is as follows:

from django.views.decorators.http import require_POST

@require_POST
def my_view(request):
pass
django.views.decorators.http.require_safe: This decorator is equivalent to a shorthand form of require_http_methods(['GET','HEAD']), which only allows access to views in a relatively safe way . Because GET and HEAD will not add, delete or modify the server. Therefore, it is a relatively safe request method. The sample code is as follows:

from django.views.decorators.http import require_safe

@require_safe
def my_view(request):
pass

Guess you like

Origin blog.csdn.net/qq_17584941/article/details/123497863