开发者

best practice for django request.method methods

开发者 https://www.devze.com 2023-02-07 15:36 出处:网络
I\'m building a REST api and one of the methods from my view needs to accept the following http methods

I'm building a REST api and one of the methods from my view needs to accept the following http methods

GET
POST
DELETE
PUT

What is the best practice to achieve this ?

So far I came up with the following

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')

thanks


Consider using django-piston. It does what you're asking for (and much more).

0

精彩评论

暂无评论...
验证码 换一张
取 消