102. The method of decorators restriction request: require_http_methods, require_GET, require_POST, require_safe

Between the client and the server requests the two most common ways:

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

Restriction request decorator:

Django built-in view to the view decorator can provide some restrictions, such as a front view only access the GET method or the like. Commonly used built-in views decorator:

1. django.http.decorators.http.require_http_methods: This decorator need to pass allows access to a list of methods.
(1) For example, if a client sends a GET request, it returns to a user interface to add the article; if the POST request is transmitted, the data to be submitted to the database will be stored. views.py file, the following sample code:
from django.views.decorators.http import require_http_methods
from django.http import HttpResponse
from django.shortcuts import render
from .models import Article


@require_http_methods(['GET', 'POST'])
def index2(request):
    <!--首先判断客户端发送的是哪种请求GET OR POST-->
    <!--注意这里一定要是“==” 只有“==”才会返回True or False-->
    if request.method == 'GET':
        return render(request,'static/add.html')
    else:
        title = request.POST.get('title')
        content = request.POST.get('content')
        price = request.POST.get('price')
        Article.objects.create(title=title, content=content, price=price)
        articles = Article.objects.all()
        return render(request, 'static/index.html', context={'articles': articles}
(2) index.html sample code is as follows:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table>
    <thead>
    <tr style="width: 20px">标题</tr>
    <tr style="width: 20px">内容</tr>
    <tr style="width: 20px">价格</tr>
    <tr style="width: 20px">创建时间</tr>
    </thead>
    <tbody>
    {% for article in articles %}
        <tr>
            <td><a href="#">{{ article.title }}</a></td>
            <td><a href="">{{ article.content }}</a></td>
            <td><a href="">{{ article.price }}</a></td>
            <td>{{ article.create_time }}</td>
        </tr>
    {% endfor %}
    </tbody>
</table>
</body>
</html>
(3) urls.py file, the following sample code:
from django.urls import path
from article import views

urlpatterns = [
    path('', views.index, name='index'),
    path('add/', views.add, name='add'),
    path('add2/', views.index2, name='add2'),
]
In Postman software, using the POST request input url: 127.0.0.1: 3000 / add2 / (here I modified the port number is 3000, default 8000) parameter title, and passed in the following URL in need of Body ., content, price returns this result:

Here Insert Picture Description

Postman software using the GET request access url: 127.0.0.1: 3000 / add2 /, will return to this page article, but the Postman, the input data even after clicking the submit button on the form do not have any reaction, it can be the browser uses the GET request access. Click on the submit button, you can see the newly added article information in the database.

Here Insert Picture Description

2. django.views.decorators.http.require_GET: decorator equivalent require_http_methods ([ 'GET']) is shorthand method GET only allowed to access the view.
(1) For example, we only allow GET request to visit the home page, views.py file in the sample code as follows:
from django.views.decorators.http import require_GET


@require_GET
def index(request):
    articles = Article.objects.all()
    return render(request, 'static/index.html', context={'articles':articles})
(2) The sample code index.html follows:
    <ul>
    {% for article in articles %}
        <li>{{% article.title %}}</li>
        <li>{{% article.content %}}</li>
        <li>{{% article.price %}}</li>
    {% endfor %}
    </ul>
3. django.views.decorators.http.require_POST: decorator equivalent require_http_methods ([ 'POST']) is shorthand method POST request is only allowed to access a view, the following sample code:
from django.views.decorators.http import require_POST


@require_POST
def add(request):
    title = request.POST.get('title')
    content = request.POST.get('content')
    Article.objects.create(title=title, content=content)
    return HttpResponse('success')
4. django.views.decorators.http.require_safe: decorator equivalent require_http_methods ([ 'GET', 'HEAD']) in the short form, only the use of relatively safe way to access the view, because the GET and HEAD not the server generating CRUD behavior, so it is a relatively safe way of requests.
Published 131 original articles · won praise 30 · views 8385

Guess you like

Origin blog.csdn.net/zjy123078_zjy/article/details/104237221