在Django Rest Framework中找不到资源时返回自定义404错误

时间:2022-04-29 23:19:04

I am learning Django Rest Framework, and also new to django. I want to return a custom 404 error in json when a client will access a resource which was not found.

我正在学习Django Rest Framework,也是django的新手。我想在客户端访问未找到的资源时在json中返回自定义404错误。

My urls.py looks liks this:

我的urls.py看起来很喜欢这个:

urlpatterns = [
    url(r'^mailer/$', views.Mailer.as_view(), name='send-email-to-admin')
]

In which i have only one resource, which can be accessed through URI, http://localhost:8000/mailer/

其中我只有一个资源,可以通过URI访问,http:// localhost:8000 / mailer /

Now, when a client access any other URI like http://localhost:8000/, API should return a 404-Not Found error like this:

现在,当客户端访问任何其他URI,如http:// localhost:8000 /时,API应返回404-Not Found错误,如下所示:

{
    "status_code" : 404
    "error" : "The resource was not found"
}

Please suggest some answer with proper code snippets, if suitable.

如果合适,请使用正确的代码段建议一些答案。

2 个解决方案

#1


13  

You are looking for handler404.

您正在寻找handler404。

Here is my suggestion:

这是我的建议:

  1. Create a view that should be called if none of the URL patterns match.
  2. 如果没有任何URL模式匹配,则创建应该调用的视图。
  3. Add handler404 = path.to.your.view to your root URLconf.
  4. 将handler404 = path.to.your.view添加到根URLconf。

Here is how it's done:

以下是它的完成方式:

  1. project.views

    project.views

    from django.http import JsonResponse
    
    
    def custom404(request):
        return JsonResponse({
            'status_code': 404,
            'error': 'The resource was not found'
        })
    
  2. project.urls

    project.urls

    from project.views import custom404
    
    
    handler404 = custom404
    

Read error handling for more details.

阅读错误处理了解更多详情。

Django REST framework exceptions may be useful as well.

Django REST框架异常也可能很有用。

#2


2  

according to django documentation : Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL. ref: https://docs.djangoproject.com/en/1.8/topics/http/urls/

根据django文档:Django按顺序运行每个URL模式,并在第一个匹配请求的URL的位置停止。 ref:https://docs.djangoproject.com/en/1.8/topics/http/urls/

so you can just add another url in urlpatterns after the one you created and it should match all url patterns and send them to a view that return the 404 code.

所以你可以在你创建的urlpatterns之后添加另一个url,它应匹配所有url模式并将它们发送到返回404代码的视图。

i.e :

即:

urlpatterns = [
url(r'^mailer/$', views.Mailer.as_view(), name='send-email-to-admin'),
url(r'^.*/$',views.Error404.as_view(),name='error404')]

#1


13  

You are looking for handler404.

您正在寻找handler404。

Here is my suggestion:

这是我的建议:

  1. Create a view that should be called if none of the URL patterns match.
  2. 如果没有任何URL模式匹配,则创建应该调用的视图。
  3. Add handler404 = path.to.your.view to your root URLconf.
  4. 将handler404 = path.to.your.view添加到根URLconf。

Here is how it's done:

以下是它的完成方式:

  1. project.views

    project.views

    from django.http import JsonResponse
    
    
    def custom404(request):
        return JsonResponse({
            'status_code': 404,
            'error': 'The resource was not found'
        })
    
  2. project.urls

    project.urls

    from project.views import custom404
    
    
    handler404 = custom404
    

Read error handling for more details.

阅读错误处理了解更多详情。

Django REST framework exceptions may be useful as well.

Django REST框架异常也可能很有用。

#2


2  

according to django documentation : Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL. ref: https://docs.djangoproject.com/en/1.8/topics/http/urls/

根据django文档:Django按顺序运行每个URL模式,并在第一个匹配请求的URL的位置停止。 ref:https://docs.djangoproject.com/en/1.8/topics/http/urls/

so you can just add another url in urlpatterns after the one you created and it should match all url patterns and send them to a view that return the 404 code.

所以你可以在你创建的urlpatterns之后添加另一个url,它应匹配所有url模式并将它们发送到返回404代码的视图。

i.e :

即:

urlpatterns = [
url(r'^mailer/$', views.Mailer.as_view(), name='send-email-to-admin'),
url(r'^.*/$',views.Error404.as_view(),name='error404')]