# -*-coding:utf-8-*-
__author__ = "GILANG (pleasurelong@foxmail.com)"
"""
django 自定义用于view的装饰器
"""
from functools import wraps def object_does_not_exist(func):
"""
不带参数的装饰器
"""
@wraps(func)
def returned_wrapper(request, *args, **kwargs):
try:
return func(request, *args, **kwargs)
except ObjectDoesNotExist:
raise Http404()
return returned_wrapper @object_does_not_exist
def detail(request):
"""
用法
"""
pass ############################################################################ def object_does_not_exist(redirect=None):
"""
第一种写法:带参数的装饰器
"""
def decorator(func):
@wraps(func)
def returned_wrapper(request, *args, **kwargs):
try:
return func(request, *args, **kwargs)
except ObjectDoesNotExist:
if redirect:
return HttpResponseRedirect(redirect)
else:
raise Http404()
return returned_wrapper
return decorator @object_does_not_exist(redirect='/')
def detail(request):
pass # 记得加个闭合括号,否则会出现类似 takes exactly 1 argument (0 given) 的错误
@object_does_not_exist()
def foo(request):
pass ############################################################################ def object_does_not_exist(func=None, redirect=None):
"""
第二种写法:带参数的装饰器
第二种方法可以解决 got an unexpected keyword argument 错误。
"""
def decorator(func):
@wraps(func)
def returned_wrapper(request, *args, **kwargs):
try:
return func(request, *args, **kwargs)
except ObjectDoesNotExist:
if redirect:
return HttpResponseRedirect(redirect)
else:
raise Http404()
return returned_wrapper if not func:
def foo(func):
return decorator(func)
return foo else:
return decorator(func) @object_does_not_exist(redirect='/')
def detail(request):
pass @object_does_not_exist
def foo(request):
pass

  

python django 自定义 装饰器的更多相关文章

  1. Django自定义装饰器

    装饰器模板: def decorator(func): def wrapper(*args,**kwargs): return func(*args,**kwargs) return wrapper ...

  2. Django学习笔记第八篇--实战练习四--为你的视图函数自定义装饰器

    零.背景: 对于登录后面所有视图函数,都需要验证登录信息,一般而言就是验证cookie里面的一些信息.所以你可以这么写函数: def personinfo(request): ": retu ...

  3. Day04 - Python 迭代器、装饰器、软件开发规范

    1. 列表生成式 实现对列表中每个数值都加一 第一种,使用for循环,取列表中的值,值加一后,添加到一空列表中,并将新列表赋值给原列表 >>> a = [0, 1, 2, 3, 4, ...

  4. 【Python】 闭包&装饰器

    python中的函数本身就是对象,所以可以作为参数拿来传递.同时其允许函数的层级嵌套定义,使得灵活性大大增加. 闭包 闭包的定义:将函数的语句块与其运行所需要的环境打包到一起,得到的就是闭包对象.比如 ...

  5. python 闭包和装饰器

    python 闭包和装饰器 一.闭包闭包:外部函数FunOut()里面包含一个内部函数FunIn(),并且外部函数返回内部函数的对象FunIn,内部函数存在对外部函数的变量的引用.那么这个内部函数Fu ...

  6. python设计模式之装饰器详解(三)

    python的装饰器使用是python语言一个非常重要的部分,装饰器是程序设计模式中装饰模式的具体化,python提供了特殊的语法糖可以非常方便的实现装饰模式. 系列文章 python设计模式之单例模 ...

  7. Python入门篇-装饰器

    Python入门篇-装饰器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.装饰器概述 装饰器(无参) 它是一个函数 函数作为它的形参 返回值也是一个函数 可以使用@functi ...

  8. python高级之装饰器

    python高级之装饰器 本节内容 高阶函数 嵌套函数及闭包 装饰器 装饰器带参数 装饰器的嵌套 functools.wraps模块 递归函数被装饰 1.高阶函数 高阶函数的定义: 满足下面两个条件之 ...

  9. [python基础]关于装饰器

    在面试的时候,被问到装饰器,在用的最多的时候就@classmethod ,@staticmethod,开口胡乱回答想这和C#的static public 关键字是不是一样的,等面试回来一看,哇,原来是 ...

随机推荐

  1. appium api

    AppiumDriver getAppStrings()      默认系统语言对应的Strings.xml文件内的数据.iOS driver.getAppStrings(Stringlanguage ...

  2. android-----JNI学习 helloworld

    (1)新建android工程 (2)添加NDK路径 (3)添加本地支持 给本地库起名 此时工程目录下会自动生成jni文件夹 此时Makefile也自动生成 LOCAL_PATH := $(call m ...

  3. StopWatch

    附件 http://download.csdn.net/detail/teststudio/6575241 主窗体UNIT unit MainForm; interface uses Windows, ...

  4. 通过MultipleOutputs写到多个文件

    MultipleOutputs 类可以将数据写到多个文件,这些文件的名称源于输出的键和值或者任意字符串.这允许每个 reducer(或者只有 map 作业的 mapper)创建多个文件. 采用name ...

  5. PL/SQL 批量SQL

    批量SQL包括: FORALL语句 BULK COLLECT子句 FORALL语句 FORALL具有如下结构: FORALL loop_counter IN bounds_clause [SAVE E ...

  6. (转)if语句优化

    一.使用常见的三元操作符  if (foo) bar(); else baz(); ==> foo?bar():baz(); if (!foo) bar(); else baz(); ==> ...

  7. Canvas模糊化处理图片、毛玻璃处理图片之stackblur.js

    Canvas实现毛玻璃效果解决方式1:使用stackblur.js 在Android系统中实现图片的毛玻璃效果比较好用的类库是:Android StackBlur 官方Git地址:https://gi ...

  8. 使用 logback + slf4j 进行日志记录

    此处主要介绍maven web工程下如何使用 logback + slf4j  进行日志记录. logback主要包含三个组成部分:Loggers(日志记录器).Appenders(输出目的在).La ...

  9. Use StringBuilder instead of String as possible as you can.

    If you are care a littile about the time your algorithm cost,you should notice that,you my use Strin ...

  10. 【转】 iOS KVO KVC

    原文: http://www.cocoachina.com/industry/20140224/7866.html Key Value Coding Key Value Coding是cocoa的一个 ...