Django request.method 方法的最佳实践

3 投票
1 回答
631 浏览
提问于 2025-04-16 10:58

我正在搭建一个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')

谢谢

1 个回答

3

可以考虑使用 django-piston。它可以满足你的需求(而且功能还很多)。

撰写回答