一:类视图
1. 为什么使用类视图?
# 以注册请求逻辑为例 def register(request): if request.method == "GET": render(request, "register.html") if request.method == "POST": name = request.POST.get("name") age = request.POST.get("age") # 加入验证字段的逻辑 # 存储数据库的逻辑
上面的一段函数里面处理了两个逻辑,get逻辑和post逻辑,也能满足业务需求,那么我们为什么要使用类视图呢?
使用类视图的好处:1.代码整洁,可读性强 2: 通过类的继承,直接可以使用类视图中的方法。
二:类视图使用的例子
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="" method="POST"> # 注意:id="username"导致,后端不能通过username的键取到输入的值。 姓名:<input type="text" name="username" placeholder="用户名"> 密码:<input type="password" name="password" placeholder="密码"> <input type="submit"> </form> </body> </html>
from django.conf.urls import url from .views import RegisterView urlpatterns = [ url(r"^register/", RegisterView.as_view()), ]
from django.http import JsonResponse from django.shortcuts import render from django.views.generic import View class RegisterView(View): """类是图处理注册""" def get(self,request): """处理get请求""" return render(request, "./register.html") def post(self,request): """处理post请求""" name = request.POST.get("username") password = request.POST.get("password") json_dict = {"name":name,"password":password} # 给前端返回一个json字符串内容 return JsonResponse(json_dict)
前端界面
三:装饰器在类视图中的应用
使用场景:页面必须登录后才能访问,那么在进入类视图,调用get方法后,实现业务逻辑之前,先使用装饰器验证一下,用户是否已经登录。
from django.http import JsonResponse from django.shortcuts import render from django.views.generic import View from django.utils.decorators import method_decorator def decoreate_login(func): def inner(request,*args,**kwargs): name = request.COOKIES.get("name") if not name: return JsonResponse({"error":"用户未登录"}) return func(request, *args,**kwargs) return inner class RegisterView(View): """类是图处理注册""" # 只是对类中的某一个方法进行了装饰,这是一种方式的装饰。 @method_decorator(decoreate_login) def get(self,request): """处理get请求""" return render(request, "./register.html") def post(self,request): """处理post请求""" name = request.POST.get("username") password = request.POST.get("password") json_dict = {"name":name,"password":password} return JsonResponse(json_dict)
from django.http import JsonResponse from django.shortcuts import render from django.views.generic import View from django.utils.decorators import method_decorator def decoreate_login(func): def inner(request,*args,**kwargs): name = request.COOKIES.get("name") if not name: return JsonResponse({"error":"用户未登录"}) return func(request, *args,**kwargs) return inner # 和上面的方式不一样,在类的外面进行装饰,name属性,指定装饰器的函数名字为get,效果都一样。 @method_decorator(decoreate_login,name="get") class RegisterView(View): """类是图处理注册""" def get(self,request): """处理get请求""" return render(request, "./register.html") def post(self,request): """处理post请求""" name = request.POST.get("username") password = request.POST.get("password") json_dict = {"name":name,"password":password} return JsonResponse(json_dict)
使用场景:禁止黑名单用户访问,例如禁止127.0.0.1的ip访问我们的视图。
from django.http import JsonResponse from django.shortcuts import render from django.views.generic import View from django.utils.decorators import method_decorator def ban_ip(func): def inner(request,*args,**kwargs): ip = request.META.get("REMOTE_ADDR") if ip in ["127.0.0.1","192.168.33.21"]: return JsonResponse({"error":"此IP已经被封,封存时间为24小时"}) return func(request, *args,**kwargs) return inner class RegisterView(View): """类是图处理注册""" def get(self,request): """处理get请求""" return render(request, "./register.html") @method_decorator(ban_ip) def post(self,request): """处理post请求""" name = request.POST.get("username") password = request.POST.get("password") json_dict = {"name":name,"password":password} return JsonResponse(json_dict)
点击提交按钮后