django request.method方法的最佳实践

时间:2011-02-01 00:09:41

标签: python django django-views

我正在构建一个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 个答案:

答案 0 :(得分:3)

考虑使用 django-piston 。它可以满足您的要求(以及更多)。