102.限制請求的method裝飾器:require_http_methods,require_GET,require_POST,require_safe


客戶端與服務器之間最常用的兩種請求方式:

1. GET請求一般是用來向服務器索取數據,但不會向服務器提交數據,不會對服務器的狀態進行更改。
2.POST請求一般是用來向服務器提交數據,會對服務器的狀態進行更改。

限制請求裝飾器:

Django內置的視圖裝飾器可以給視圖提供一下限制,比如正視圖只能通過GET的method訪問等。常用的內置視圖裝飾器:

1. django.http.decorators.http.require_http_methods: 這個裝飾器需要傳遞一個允許訪問的方法的列表。
(1)比如,如果客戶端發送的是GET請求,就返回給用戶一個添加文章的界面;如果發送的是POST請求,就將提交的數據進行保存到數據庫中。views.py文件中示例代碼如下:
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示例代碼如下:
<!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文件中示例代碼如下:
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'),
]
在Postman軟件中,采用POST請求輸入url:127.0.0.1:3000/add2/(在這里我修改了端口號為3000,默認情況為8000),並且在URL下面的Body中傳入需要的參數值title,content,price.返回這樣的結果:

在這里插入圖片描述

在Postman軟件采用GET請求訪問url:127.0.0.1:3000/add2/,就會返回添加文章的頁面,但是在Postman中,即使在表單中輸入了數據點擊提交按鈕之后也沒有任何反應,所以可以在瀏覽器中使用GET請求訪問。點擊提交按鈕,就可以在數據庫中看到新添加的文章信息了。

在這里插入圖片描述

2. django.views.decorators.http.require_GET: 這個裝飾器相當於是require_http_methods(['GET'])的簡寫形式,只允許使用GET的method來訪問視圖。
(1)比如,我們只允許GET請求訪問首頁,views.py文件中示例代碼如下:
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)index.html中示例代碼如下:
    <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: 這個裝飾器相當於是require_http_methods(['POST'])的簡寫形式,只允許使用POST請求的method來訪問視圖,示例代碼如下:
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: 這個裝飾器相當於是require_http_methods(['GET', 'HEAD'])的簡寫形式,只允許使用相對安全的方式來訪問視圖,因為GET和HEAD不會對服務器產生增刪改的行為,因此這是一種相對安全的請求方式。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM