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
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.
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.
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.
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.
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.
"""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With