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

yipeiwu_com7年前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向C语言的链接库传递数组、结构体、指针类型的数据

使用python向C语言的链接库传递数组、结构体、指针类型的数据 由于最近的项目频繁使用python调用同事的C语言代码,在调用过程中踩了很多坑,一点一点写出来供大家参考,我们仍然是使用...

简介Python的collections模块中defaultdict类型的用法

defaultdict 主要用来需要对 value 做初始化的情形。对于字典来说,key 必须是 hashable,immutable,unique 的数据,而 value 可以是任意的...

python获取mp3文件信息的方法

本文实例讲述了python获取mp3文件信息的方法。分享给大家供大家参考。具体如下: 将代码生成.py文件放在目录下运行,可以获取该目录的所有mp3文件的信息,需要使用ID3库 im...

tensorflow实现tensor中满足某一条件的数值取出组成新的tensor

tensorflow实现tensor中满足某一条件的数值取出组成新的tensor

首先使用tf.where()将满足条件的数值索引取出来,在numpy中,可以直接用矩阵引用索引将满足条件的数值取出来,但是在tensorflow中这样是不行的。所幸,tensorflow...

Windows下anaconda安装第三方包的方法小结(tensorflow、gensim为例)

Windows下anaconda安装第三方包的方法小结(tensorflow、gensim为例)

 anaconda 集成了很多科学计算中所需要的包,如numpy,scipy等等,具体查看anaconda中已经预先安装配置好的包有哪些,可以通过cmd命令,输入conda l...