2013-08-30 33 views

回答

1

您應該覆蓋資源中的dispatch方法,並在那裏創建每個函數。如果你想做一些簡單的邏輯,你可以在調用原始資源dispatch後放置代碼。該守則將是這個樣子:

def dispatch(self, request_type, request, **kwargs): 
    response = super(Resource, self).dispatch(request_type, request, **kwargs) 

    # Pass any parameters that you require to the functions 
    if request.method == 'GET': 
     custom_get() 
    if request.method == 'POST': 
     custom_post() 
    if request.method == 'PUT': 
     custom_put() 
    if request.method == 'DELETE': 
     custom_delete() 

    return response 

一般來講應該足夠你的目的,如果你想要做一些更復雜的東西與響應除。

相關問題