102:限制请求method装饰器

常用的请求method:

  • GET请求:GET请求一般用来向服务器索取数据,但不会向服务器提交数据,不会对服务器的状态进行更改。比如向服务器获取某篇文章的详情。
  • POST请求:POST请求一般是用来向服务器提交数据,会对服务器的状态进行更改。比如提交一篇文章给服务器。

限制请求装饰器:

Django内置的视图装饰器可以给视图提供一些限制。比如这个视图只能通过GETmethod访问等。以下将介绍一些常用的内置视图装饰器。

1、django.http.decorators.http.require_http_methods:这个装饰器需要传递一个允许访问的方法的列表。比如只能通过GET的方式访问。那么示例代码如下:

 from django.views.decorators.http import require_http_methods

 @require_http_methods(["GET"])
 def my_view(request):
     pass

2、django.views.decorators.http.require_GET:这个装饰器相当于是require_http_methods(['GET'])的简写形式,只允许使用GETmethod来访问视图。示例代码如下:

 from django.views.decorators.http import require_GET

 @require_GET
 def my_view(request):
     pass

3、django.views.decorators.http.require_POST:这个装饰器相当于是require_http_methods(['POST'])的简写形式,只允许使用POSTmethod来访问视图。示例代码如下:

 from django.views.decorators.http import require_POST

 @require_POST
 def my_view(request):
     pass

4、django.views.decorators.http.require_safe:这个装饰器相当于是require_http_methods(['GET','HEAD'])的简写形式,只允许使用相对安全的方式来访问视图。因为GETHEAD不会对服务器产生增删改的行为。因此是一种相对安全的请求方式。示例代码如下:

 from django.views.decorators.http import require_safe

 @require_safe
 def my_view(request):
     pass

示例工程如下:

 modele.py:

class Article(models.Model):
    title = models.CharField(max_length=100)
    contents = models.TextField()
    price = models.FloatField(default=0)
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'article'

views.py:

from django.shortcuts import render
from .models import Article
from django.http import HttpResponse
from django.views.decorators.http import require_http_methods, require_GET, require_POST, require_safe

# Create your views here.

@require_GET
def index(request):
    articles = Article.objects.all()
    return render(request, 'index.html', context={'articles':articles})

@require_http_methods(["GET","POST"])
def add_article(request):
    if request.method == "GET":
        return render(request,'add_article.html')
    else:
        title = request.POST.get('title')
        contents = request.POST.get('content')
        Article.objects.create(title=title, contents=contents)
        return HttpResponse('success')

urls.py:

from article import views

urlpatterns = [
    path('', views.index),
    path('add_article/', views.add_article, name='add_article'),
]

index.html:

<body>
    <ul>
        {% for article in articles %}
            <li>{{ article.title }}</li>
        {% endfor %}

    </ul>
</body>

add_article.html:

<body>
<form action="{% url 'add_article' %}" method="post">
        <tables>
            <tbody>
                <tr>
                    <td>标题:</td>
                    <td><input type="text", name="title"></td>
                </tr>
                <tr>
                    <td>内容:</td>
                    <td>
                        <textarea name="content" id="" cols="30" rows="10"></textarea>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit", name="提交"></td>
                </tr>
            </tbody>
    </tables>
</form>
</body>
原文地址:https://www.cnblogs.com/zheng-weimin/p/10404036.html