2014-01-16 45 views
-1
from django.views.decorators.http import require_http_mothods 

@require_http_methods(["GET", "POST"]) 
def my_view(request): 
    pass 

在上面的例子中有一个“@”。但我无法弄清楚。 在此先感谢。 :)“def”上面的“@”是什么意思?

+2

它的[装饰]启动(http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Decorators)。 –

+0

(Protip:我搜索了“'@”[python]'。) – user2864740

回答

1

@用于decorate一个函数。这种机制被称为decorator

装饰器是一个函数,它将修改另一个函数的行为。

对于您的情况,require_http_methods装饰器在调用my_view函数之前检查请求是GET还是POST方法。

这是一个非常强大的机制,我建议花一点时间来理解它。您可以使用此tutorial

我希望它能帮助