【发布时间】:2012-08-02 19:24:23
【问题描述】:
我有意见:
@decorator
def func(request):
hello = "hello"
return render_to_responce("test.html", locals() )
和模板test.html:
{{ hello }}
{{ username }}
我想为func(request) 编写装饰器,它将一个变量USERNAME 添加到函数中,并在模板中返回两个参数。我试图使它如下:
def decorator(func):
def wrapper( request, *args, **kwargs):
username = request.user.username
q = func(request, *args, **kwargs)
#what I need add here I do not know ...
return q
return wrapper
【问题讨论】:
-
你认为你为什么想要一个装饰器?你能解释一下用例吗? (您是否也可以清理您的代码,使其语法正确且更具可读性)?