我有一个基于类的通用视图:
class ProjectDetails(mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
generics.GenericAPIView):
queryset = Project.objects.all()
# Rest of definition
在我的urls.py
中,我有:
当使用不存在的id调用API时,它将返回HTTP 404
响应,其中包含以下内容:
{
"detail": "Not found."
}
是否可以修改此响应?在
我需要为这个视图自定义错误消息。在
此解决方案影响所有视图:
当然,您可以提供自定义异常处理程序:Custom exception handling
确定您可以跳过默认值
rest_framework.views.exception_handler
并使其完全原始。在注意:请记住在
django.conf.settings.REST_FRAMEWORK['EXCEPTION_HANDLER']
中提到您的处理程序针对特定视图的解决方案:
^{pr2}$可以通过将
update
,retrieve
等特定方法重写为:相关问题 更多 >
编程相关推荐