[Fixed]-Difference between Response and HttpResponse django

2👍

drf’s Response subclasses django’s SimpleTemplateResponse. SimpleTemplateResponse subclasses HttpResponse. That is to say, Response has more features than HttpResponse.

  1. Response provides a Web browsable API, a huge usability win for developers.

  2. Response can handle native Python primitives such as dict, list and str. However, HTTPResponse only supports str, if you return is dict or list, HTTPResponse will convert them. And you will find the converted str is not what you want.

Here is the difference what I have learned so far.

👤haojie

1👍

HttpResponse->SimpleTemplateResponse->Response

code:

"""
The Response class in REST framework is similar to HTTPResponse, except that
it is initialized with unrendered data, instead of a pre-rendered string.

The appropriate renderer is called during Django's template response rendering.
"""
class Response(SimpleTemplateResponse):
    """
    An HttpResponse that allows its data to be rendered into
    arbitrary media types.
    """
👤Ykh

0👍

In Django, whether you use Response or HttpResponse, or JSONResponse, it will become an incoming Response object inside the middleware.

Django’s internal components use Response objects to communicate with each other.

👤kta

-3👍

You shouldn’t use libraries without reading their documentation.

Response is from Django Rest Framework, not Django, and is fully documented there.

Leave a comment