-1
from django.views.decorators.http import require_http_mothods
@require_http_methods(["GET", "POST"])
def my_view(request):
pass
在上面的例子中有一個「@」。但我無法弄清楚。 在此先感謝。 :)「def」上面的「@」是什麼意思?
from django.views.decorators.http import require_http_mothods
@require_http_methods(["GET", "POST"])
def my_view(request):
pass
在上面的例子中有一個「@」。但我無法弄清楚。 在此先感謝。 :)「def」上面的「@」是什麼意思?
@
用於decorate
一個函數。這種機制被稱爲decorator
。
裝飾器是一個函數,它將修改另一個函數的行爲。
對於您的情況,require_http_methods
裝飾器在調用my_view
函數之前檢查請求是GET還是POST方法。
這是一個非常強大的機制,我建議花一點時間來理解它。您可以使用此tutorial
我希望它能幫助
它的[裝飾]啓動(http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Decorators)。 –
(Protip:我搜索了「'@」[python]'。) – user2864740