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
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
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 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.
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.
"""
You shouldn't use libraries without reading their documentation.
Response is from Django Rest Framework, not Django, and is fully documented there.
© 2022 - 2024 — McMap. All rights reserved.
Response
is in Django any longer - at least as of 1.11+ (if it was before, I am not sure). – Midi