The Response
class is a subclass of Django’s standard HttpResponse
. It simplifies returning JSON responses by handling content negotiation and rendering the data into the requested content type.
Basic Usage:
from rest_framework.response import Response
from rest_framework.decorators import api_view
@api_view(['GET'])
def example_view(request):
data = {'message': 'Hello, world!'}
return Response(data)
Attributes:
200 OK
.Status Codes:
You can set the status code using the status
attribute:
from rest_framework import status
@api_view(['GET'])
def example_view(request):
data = {'message': 'Hello, world!'}
return Response(data, status=status.HTTP_200_OK)
DRF handles content negotiation, which means it can return responses in various formats based on the client’s request. By default, it supports JSON, but you can add other renderers like YAML or XML.
Example with JSON:
from rest_framework.renderers import JSONRenderer
@api_view(['GET'])
def example_view(request):
data = {'message': 'Hello, world!'}
response = Response(data)
response.accepted_renderer = JSONRenderer()
response.accepted_media_type = 'application/json'
return response
You can add custom headers to the response:
@api_view(['GET'])
def example_view(request):
data = {'message': 'Hello, world!'}
response = Response(data)
response['X-Custom-Header'] = 'CustomValue'
return response
You can return HTML responses using Django templates:
from django.shortcuts import render
@api_view(['GET'])
def example_view(request):
data = {'message': 'Hello, world!'}
return render(request, 'template.html', data)
To handle various HTTP status codes:
from rest_framework import status
@api_view(['POST'])
def example_view(request):
if not request.data.get('key'):
return Response({'error': 'Bad Request'}, status=status.HTTP_400_BAD_REQUEST)
data = {'message': 'Success'}
return Response(data, status=status.HTTP_201_CREATED)
You can redirect responses using Django’s HttpResponseRedirect
:
from django.http import HttpResponseRedirect
@api_view(['GET'])
def example_view(request):
return HttpResponseRedirect('/some-path/')
For large responses, use StreamingHttpResponse
:
from django.http import StreamingHttpResponse
def generator():
yield 'Part 1'
yield 'Part 2'
@api_view(['GET'])
def example_view(request):
return StreamingHttpResponse(generator())
To serve files:
from django.http import FileResponse
@api_view(['GET'])
def example_view(request):
file = open('example.txt', 'rb')
return FileResponse(file)