2011-02-01 99 views
2

我建立一個REST API,並從我的觀點的一種方法需要接受以下HTTP方法最佳實踐request.method方法

GET 
POST 
DELETE 
PUT 

什麼是實現這一目標的最佳做法?

到目前爲止,我想出了以下

with_id_storage = { 
'GET' : _with_id_get, 
'POST' : _with_id_post, 
'PUT' : _with_id_put, 
'DELETE': _with_id_delete, 
} 

def with_id(request, id): 

try: 
    log.info('calling %s' % request.method) 
    return with_id_storage[request.method](request, test_id) 
except KeyError: 
    return HttpResponse('Not ready yet') 

感謝

回答

3

考慮使用django-piston。它可以滿足你的要求(還有更多)。

+1

+1:不要重新發明這個輪子。 – 2011-02-01 00:25:22