Click to jump to the REST-Framework column directory

Response

When you need to customize the return Response object, you may need to use the Response class. You just need to import it from the framework to the required service module:

from rest_framework.response import Response
Copy the code

To instantiate an object you may need the following parameters:

  • data: Serialized data for the response
  • status: Status code of the response
  • template_name: Specifies the name of the template to use
  • headers: Specifies the HTTP header to be added to the response
  • content_type: The type of response content, which is usually set automatically unless you need to explicitly specify it manually

.data

Gets the serialized data for the response instance object.

.status_code

Gets the status code of the response instance.

.content

When you call the.render() method first, you can use this property to get the content data that the response will render.

.template_name

Use this property to access the template name when you use the template renderer.

.accepted_renderer

The instance object used to render the response.

.accepted_media_type

The type used to get the data content.

.renderer_context

Dictionary data with additional context information, which is passed to the renderer’s.render() method.

rander()

As with any TemplateResponse method, this method is called to render the serialized data of the response as the final response content. When.render() is called, the response content will be set to.render(data, accepted_media_type, Renderer_context) to an accepted_renderer instance.