difference between Response and HttpResponse django
Asked Answered
D

3

18

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
Debtor answered 9/11, 2017 at 2:35 Comment(2)
You should show your import paths and which Django version, as I don't believe Response is in Django any longer - at least as of 1.11+ (if it was before, I am not sure).Midi
i still use Response in django 1.11 requirements.txt Django==1.11.5 django-cors-headers==2.1.0 django-filter==1.0.4 djangorestframework==3.6.4 djangorestframework-filters==0.10.1 djangorestframework-jwt==1.11.0Debtor
B
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.

Bisque answered 21/4, 2022 at 5:43 Comment(0)
F
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.
    """
Freezedrying answered 9/11, 2017 at 3:2 Comment(0)
L
-4

You shouldn't use libraries without reading their documentation.

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

Libertylibia answered 9/11, 2017 at 8:29 Comment(1)
This might be a more appropriate link: django-rest-framework.org/api-guide/responsesRepand

© 2022 - 2024 — McMap. All rights reserved.