I'm using Django REST framework to create an API that supports JSON and CSV output.
I have this line in my urls.py:
url(r'^api/events/$', views.EventsView.as_view(), name='events'),
EventsView looks like this:
class EventsView(APIView):
def dispatch(self, request, *args, **kwargs):
return super(EventsView, self).dispatch(request, *args, **kwargs)
def get(self, request):
logger.info("Here")
events = EventsQuery(request)
if events.is_valid():
events.build_response()
return events.get_response()
If I visit /api/events/?format=json I get a set of results as valid JSON, and I see "Here" logged to my log file.
If I visit /api/events/?format=csv I get a 404 response with a JSON body of
{
"detail": "Not found."
}
...and nothing is logged.
The lack of logging is what's throwing me. It's like it's not even getting to the EventsView class, but how could changing a querystring value in the URL stop it being routed to that class? And how do I find out where it IS being routed to?
Edit: The content of EventsQuery.get_response() is:
def get_response(self):
if self.has_error:
self.response = {
'success': self.success,
'errors': self.errors
}
resp_status = status.HTTP_400_BAD_REQUEST
else:
resp_status = status.HTTP_200_OK
return Response(
self.response, status=resp_status, content_type=self.content_type
)