2👍
drf’s Response
subclasses django’s SimpleTemplateResponse
. SimpleTemplateResponse
subclasses HttpResponse
. That is to say, Response
has more features than HttpResponse
.
-
Response
provides a Web browsable API, a huge usability win for developers. -
Response
can handle native Python primitives such asdict
,list
andstr
. However,HTTPResponse
only supportsstr
, if you return isdict
orlist
,HTTPResponse
will convert them. And you will find the convertedstr
is not what you want.
Here is the difference what I have learned so far.
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.
"""
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.
- How to resolve the "psycopg2.errors.UndefinedTable: relation "auth_user" does not exist" when running django unittests on Travis
- Passing an object created with SubFactory and LazyAttribute to a RelatedFactory in factory_boy
- Force delete of any previous test database (autoclobber) when running Django unit tests, eg, in PyCharm
-3👍
You shouldn’t use libraries without reading their documentation.
Response is from Django Rest Framework, not Django, and is fully documented there.