Django REST exception throwing

I am trying to check for exceptions as part of django rest. Based on http://www.django-rest-framework.org/api-guide/exceptions/ raising NotFound,

By default, this exception results in a response with the HTTP status code "404 not found".

However, when I issue a GET (to / example1), I get 500 s,

Request Method: GET Request URL: http://192.168.59.103:8002/example1 Django Version: 1.8.3 Exception Type: NotFound Exception Value: not found Exception Location: /home/djangoapp/testtools/api/views.py in example1, line 7 Python Executable: /usr/bin/python 

below,

settings.py

 REST_FRAMEWORK = {'EXCEPTION_HANDLER':'rest_framework.views.exception_handler'} INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', ) 

urls.py

 from django.conf.urls import include, url from api import views urlpatterns = [ url(r'example1', views.example1), ] 

views.py

 from rest_framework import status from rest_framework.decorators import api_view from rest_framework.response import Response from rest_framework.exceptions import APIException,ParseError,NotFound def example1(request): raise NotFound("not found") 

Any ideas?

+4
source share
3 answers
 from rest_framework.exceptions import NotFound 

The NotFound class extends from an APIException . The default status code for APIException is 500 Internal Server Error , while for NotFound there is 404 .

So, in my opinion, you are trying to throw a rest framework exception in a pure django view. I wrote a test case here to check what are the chances of getting an error message. Guess that a simple django view trying to raise a rest framework exception is not recognized as a view error. But when providing the decorator and declaring its View API, it introduces exception_handler

So when you do this:

 from rest_framework.decorators import api_view @api_view() def example1(request): raise NotFound('not found') 

Now it is recognized as an exception created by the API, which introduces the default rest_framework exception_handler provided by you, where it returns the response for any given exception.

His docstring says:

Gets the response that should be used for any given exception. From the default, we handle the REST APIException structure as well as Django's built-in ValidationError , Http404 and PermissionDenied exceptions. Any unhandled exceptions may return None , which causes a 500 error.

+12
source

I'm not sure why you are doing this. This is an internal exception used by DRF when it cannot find a resource. But you use it in the standard Django view, outside of any of the DRF mechanisms. If you want to do this, you must use the standard Django exception:

 from django.core.exceptions import Http404 def example1(request): raise Http404('not found') 
+1
source

Try changing your url with a reqular expression that matches all possible urls and put it at the end of the main urls.py

for ex:

 urlpatterns = [ url(r'^(?P<slug>[\w-]+)/', views.example1), ] 
0
source

All Articles