Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between Response and HttpResponse django

Tags:

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
like image 492
Kent Dela Cruz Fueconcillo Avatar asked Nov 09 '17 02:11

Kent Dela Cruz Fueconcillo


People also ask

What is the difference between HttpResponse and render in Django?

Abid covers it, render is usually used to load a template and a context, while HttpResponse is usually for data. As it's bad practice to "respond" with html. Render is essentially a shortuct for HttpResponse , It provides a more effective way to modify templates, and load data dynamically.

What is HttpResponse in Django?

HttpResponse Methods – DjangoIt is used to set the given header name to the given value. HttpResponse.__delitem__(header) It deletes the header with the given name. HttpResponse.__getitem__(header) It returns the value for the given header name.

What is the difference between HttpResponse and HttpResponseRedirect?

According to the docs, render Combines a given template with a given context dictionary and returns an HttpResponse object with that rendered text , while HttpResponseRedirect returns an HTTP status code 302 [redirect] along with the new URL.

What is request argument in Django?

Django uses request and response objects to pass state through the system. When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function.


1 Answers

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.
    """
like image 133
Ykh Avatar answered Sep 28 '22 09:09

Ykh