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
The unrendered, serialized data for the response. Should consist of native Python primitives.
HTTP status code for the response. Defaults to 200 OK.
Template name to use if
HTMLRenderer is selected. Only required for template responses.Dictionary of HTTP headers to include in the response.
Boolean indicating if this response is for an exception. Used internally by the exception handler.
The content type of the response. Typically set automatically by the renderer.
Creating Responses
Attributes
.data
Response data as Python primitives (dict, list, string, etc.)
.status_code
HTTP status code (200, 404, 500, etc.)
.status_text
Status reason phrase (e.g., “OK”, “Not Found”, “Internal Server Error”)
.content
.render() method must be called before accessing this property.
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
HTMLRenderer or a custom template renderer is used.
Name of the template to render
.accepted_renderer
APIView or @api_view before the response is returned.
Renderer instance (e.g., JSONRenderer, BrowsableAPIRenderer)
.accepted_media_type
APIView or @api_view.
Media type string (e.g., “application/json”, “text/html”)
.renderer_context
.render() method.
Context dictionary containing view, request, and other rendering context
Standard HttpResponse Methods
TheResponse class extends Django’s SimpleTemplateResponse, so all standard attributes and methods are available.
Setting Headers
You can set headers using dictionary-style access:.render()
You typically don’t need to call
.render() yourself - it’s handled automatically.Example Usage
Basic Response
Response with Status Code
Response with Custom Headers
Template Response
Content Negotiation
TheResponse class works seamlessly with DRF’s content negotiation:
Accept: application/json→ Returns JSONAccept: application/xml→ Returns XML (if XMLRenderer configured)Accept: text/html→ Returns browsable API (if BrowsableAPIRenderer configured)
See Also
- Request - Request class
- Status Codes - HTTP status code constants
- Renderers - Renderer classes
