django如何通过类视图使用装饰器

yipeiwu_com5年前Python基础

需求:当我们想禁止ip黑名单访问我们的某些页面时,例如注册页面。应该怎么操作呢?

解决方案:这时候我们可以设计一个装饰器,过滤ip黑名单。

装饰器的写法如下:

from functools import wraps


def ban_ip(func):
 @wraps(func)
 def wrapper(request,*args,**kwargs):
 IP = request.META.get("REMOTE_ADDR")
 if IP in ["127.0.0.1"]: # 可以添加ip黑名单到列表里
  return HttpResponse("禁止访问")
 return func
 return wrapper

如果不是类视图,可以这样使用:

@ban_ip
def get_register(request):
 return render(request,"注册页面.html")


def post_resiger(request):
 name = request.POST.get("name")
 password = request.POST.get("password")
 print(name)
 print(password)
 return HttpResponse("注册成功")

这样就可以达到一个效果。如果ip在黑名单里,那么访问注册页面时会弹出“禁止访问”。

需求:但是,问题来了,当我们使用类视图时,定义视图函数时还有一个self参数。因为装饰器没有传这个参,所以会报错。应该怎么办呢?

解决办法:

(1)给类视图中特定的方法

class register(View):
 @method_decorator(ban_ip)
 def get(self,request):
 return render(request,"注册页面.html")

 def post(self,request):
 name = request.POST.get("name")
 password = request.POST.get("password")
 print(name)
 print(password)
 return HttpResponse("注册成功")

解读:

  • 注意:针对函数定义的装饰器,不能直接应用到类视图的方法中,因为少了一个self参数
  • 解决:可以使用method_decorator装饰器,为函数装饰器补充第一个self参数,使它可以应用到类的方法中。

(2)给所有类视图的所有方法应用装饰器

类视图的实现是通过as_view()这个方法来分发不同的请求来实现了。我们可以利用这点来给类视图的所有方法都应用装饰器。

一个比较简单但是不实用的办法就是在项目的urls.py上面来实现。在分发请求的前面加上装饰器。

url(r'^register$',ban_ip(views.register.as_view()))

虽然这样比较方便,但是太不美观了。装饰器的优雅都被它破坏了,那我们应该怎么办呢?

解决办法:把as_view()的方法重写一下,给它加入装饰器的功能。

class register(View):
 
 # 重写请求分发方法,as_view()的核心语句
 @method_decorator(ban_ip)
 def dispatch(self, request, *args, **kwargs):
 return super().dispatch(request, *args, **kwargs)

 def get(self,request):
 return render(request,"注册页面.html")

 def post(self,request):
 name = request.POST.get("name")
 password = request.POST.get("password")
 print(name)
 print(password)
 return HttpResponse("注册成功")

(3)对类视图的某个方法应用装饰器

@method_decorator(ban_ip,name='get')
class register(View):

 def get(self,request):
 return render(request,"post.html")

 def post(self,request):
 title = request.POST.get("title")
 content = request.POST.get("content")
 print(title)
 print(content)
 return HttpResponse("发布成功")

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

利用python开发app实战的方法

利用python开发app实战的方法

我很早之前就想开发一款app玩玩,无奈对java不够熟悉,之前也没有开发app的经验,因此一直耽搁了。最近想到尝试用python开发一款app,google搜索了一番后,发现确实有路可寻...

详解Django缓存处理中Vary头部的使用

Vary 头部定义了缓存机制在构建其缓存键值时应当将哪个请求头标考虑在内。 例如,如果网页的内容取决于用户的语言偏好,该页面被称为根据语言而不同。 缺省情况下,Django 的缓存系统使...

python通过robert、sobel、Laplace算子实现图像边缘提取详解

python通过robert、sobel、Laplace算子实现图像边缘提取详解

实现思路:   1,将传进来的图片矩阵用算子进行卷积求和(卷积和取绝对值)   2,用新的矩阵(与原图一样大小)去接收每次的卷积和的值   3,卷积图片所有的像素点后,把新的矩阵数据类型...

Unicode和Python的中文处理

在Python语言中,Uincode字符串处理一直是一个容易让人迷惑的问题。许多Python爱好者经常因为搞不清Unicode、UTF-8还有其它许许多多的编码之间的区别而大伤脑筋。笔者...

python bluetooth蓝牙信息获取蓝牙设备类型的方法

python 获取蓝牙设备类型 扫描蓝牙设备获取到的信息中,无法判断扫描到的蓝牙设备属于什么类型的设备。 扫描蓝牙信息使用的是python 里面的bluetooth模块。 首先扫描出来的...