2011-02-01 103 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