客戶端與服務器之間最經常使用的兩種請求方式:
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不會對服務器產生增刪改的行爲,所以這是一種相對安全的請求方式。