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_methodsfrom django.http import HttpResponsefrom django.shortcuts import renderfrom .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 pathfrom article import viewsurlpatterns = [ 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_GETdef 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_POSTdef 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不会对服务器产生增删改的行为,因此这是一种相对安全的请求方式。

相关文章