Skip to main content
The Response class allows you to return content that can be rendered into multiple content types, depending on the client request. Unlike basic HttpResponse objects, Response objects are initialized with unrendered data.

Class Signature

from rest_framework.response import Response

Response(
    data=None,
    status=None,
    template_name=None,
    headers=None,
    exception=False,
    content_type=None
)
data
any
default:"None"
The unrendered, serialized data for the response. Should consist of native Python primitives.
status
int
default:"200"
HTTP status code for the response. Defaults to 200 OK.
template_name
str
default:"None"
Template name to use if HTMLRenderer is selected. Only required for template responses.
headers
dict
default:"None"
Dictionary of HTTP headers to include in the response.
exception
bool
default:"False"
Boolean indicating if this response is for an exception. Used internally by the exception handler.
content_type
str
default:"None"
The content type of the response. Typically set automatically by the renderer.
Do not pass a Serializer instance as data. You must pass serializer.data instead, which returns the serialized representation.

Creating Responses

from rest_framework.response import Response
from rest_framework import status

# Simple response
return Response({'message': 'Hello, world!'})

# Response with custom status code
return Response(
    {'error': 'Not found'},
    status=status.HTTP_404_NOT_FOUND
)

# Response with custom headers
return Response(
    {'result': 'success'},
    headers={'X-Custom-Header': 'value'}
)

Attributes

.data

response.data
The unrendered, serialized data of the response.
data
any
Response data as Python primitives (dict, list, string, etc.)
response = Response({'key': 'value'})
print(response.data)  # {'key': 'value'}

.status_code

response.status_code
The numeric HTTP status code of the response.
status_code
int
HTTP status code (200, 404, 500, etc.)
response = Response(status=201)
print(response.status_code)  # 201

.status_text

response.status_text
Returns the reason text corresponding to the HTTP status code.
status_text
str
Status reason phrase (e.g., “OK”, “Not Found”, “Internal Server Error”)
response = Response(status=404)
print(response.status_text)  # "Not Found"

.content

response.content
The rendered content of the response. The .render() method must be called before accessing this property.
content
bytes
Rendered response content as bytes
The .render() method is called automatically by Django’s response cycle. You typically don’t need to call it yourself.

.template_name

response.template_name
The template name, if supplied. Only required if HTMLRenderer or a custom template renderer is used.
template_name
str | None
Name of the template to render

.accepted_renderer

response.accepted_renderer
The renderer instance that will be used to render the response. Set automatically by APIView or @api_view before the response is returned.
accepted_renderer
BaseRenderer
Renderer instance (e.g., JSONRenderer, BrowsableAPIRenderer)

.accepted_media_type

response.accepted_media_type
The media type selected by the content negotiation stage. Set automatically by APIView or @api_view.
accepted_media_type
str
Media type string (e.g., “application/json”, “text/html”)

.renderer_context

response.renderer_context
Dictionary of additional context information passed to the renderer’s .render() method.
renderer_context
dict
Context dictionary containing view, request, and other rendering context

Standard HttpResponse Methods

The Response class extends Django’s SimpleTemplateResponse, so all standard attributes and methods are available.

Setting Headers

You can set headers using dictionary-style access:
response = Response({'message': 'Success'})
response['Cache-Control'] = 'no-cache'
response['X-Custom-Header'] = 'custom-value'

.render()

response.render()
Renders the serialized data into the final response content. This method is called automatically by Django’s response cycle.
You typically don’t need to call .render() yourself - it’s handled automatically.

Example Usage

Basic Response

from rest_framework.decorators import api_view
from rest_framework.response import Response

@api_view(['GET'])
def hello_world(request):
    return Response({'message': 'Hello, world!'})

Response with Status Code

from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

class ItemCreate(APIView):
    def post(self, request):
        serializer = ItemSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(
                serializer.data,
                status=status.HTTP_201_CREATED
            )
        return Response(
            serializer.errors,
            status=status.HTTP_400_BAD_REQUEST
        )

Response with Custom Headers

@api_view(['GET'])
def download_file(request):
    data = {'filename': 'example.txt', 'content': '...'}
    response = Response(data)
    response['Content-Disposition'] = 'attachment; filename="example.txt"'
    return response

Template Response

from rest_framework.response import Response

def profile_view(request):
    data = {'user': request.user}
    return Response(
        data,
        template_name='profile.html'
    )

Content Negotiation

The Response class works seamlessly with DRF’s content negotiation:
@api_view(['GET'])
def data_view(request):
    data = {'items': [1, 2, 3]}
    # Will be rendered as JSON, XML, etc. based on client's Accept header
    return Response(data)
Client requests with different Accept headers:
  • Accept: application/json → Returns JSON
  • Accept: application/xml → Returns XML (if XMLRenderer configured)
  • Accept: text/html → Returns browsable API (if BrowsableAPIRenderer configured)

See Also

Build docs developers (and LLMs) love