'difference between Response and HttpResponse django
What is the difference between Response and HttpResponse in django? I am a bit confused.
from rest_framework.response import Response
Return Respose
and
from django.http import HttpResponse
return HttpResponse
Solution 1:[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.
"""
Solution 2:[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.
Solution 3:[3]
You shouldn't use libraries without reading their documentation.
Response is from Django Rest Framework, not Django, and is fully documented there.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ykh |
Solution 2 | jasonshu |
Solution 3 | Daniel Roseman |