Django request.method 方法的最佳实践
我正在搭建一个REST API,其中一个视图的方法需要接受以下的HTTP请求方式
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')
谢谢