Skip to main content
 首页 » 编程设计

python之django request.method 方法的最佳实践

2024年09月03日32qlqwjy

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

谢谢

请您参考如下方法:

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