2011-02-01 2 views
2

나는 REST API를 구축하고 있는데 내 뷰에서 메소드 중 하나가 다음 http 메소드를 받아 들일 필요가있다.django request.method 메소드의 모범 사례

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

감사

답변

3

django-piston 사용하여 다음 사항을 고려

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

+1 :이 바퀴를 재발 명하지 마십시오. –