我正在使用django rest框架来实现一个简单的api。在我的view.py里面我有这个方法:
def business_profile_detail(request, pk):
"""
Retrieve, update or delete a profile.
"""
try:
profile = BusinessProfile.objects.get(pk=pk)
except BusinessProfile.DoesNotExist:
return HttpResponse(status=404)
if request.method == 'GET':
serializer = BusinessProfileSerializer(profile)
return JSONResponse(serializer.data)
elif request.method == 'PUT':
"""
Method not supported yet
"""
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
elif request.method == 'DELETE':
"""
Method not supported yet
"""
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
因此,对于PUT和DELETE方法,我想返回错误405,但是使用django测试服务器,我一直收到错误500:
[17/Oct/2014 18:20:22] "DELETE /business/api/profile/2 HTTP/1.1" 500 60487
任何人都知道为什么?
谢谢!
编辑:
Traceback:
File "/Users/*******/Documents/dev/offers/project/offers/venv/lib/python2.7/site-packages/Django-1.7-py2.7.egg/django/core/handlers/base.py" in get_response
137. response = response.render()
File "/Users/*******/Documents/dev/offers/project/offers/venv/lib/python2.7/site-packages/Django-1.7-py2.7.egg/django/template/response.py" in render
103. self.content = self.rendered_content
File "/Users/mariopersonal/Documents/dev/offers/project/offers/venv/lib/python2.7/site-packages/rest_framework/response.py" in rendered_content
49. assert renderer, ".accepted_renderer not set on Response"
Exception Type: AssertionError at /business/api/profile/2
Exception Value: .accepted_renderer not set on Response
答案 0 :(得分:1)
看起来你在那里使用的Response
类需要一个数据参数:
签名:
Response(data, status=None, template_name=None, headers=None, content_type=None)
http://www.django-rest-framework.org/api-guide/responses
我只是在那里使用基本HttpResponse
。