Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpResponse vs. Render

Tags:

http

django

I was looking over some code and came to this question -- Django: What is the difference b/w HttpResponse vs HttpResponseRedirect vs render_to_response -- which discusses the different types of request responses.

Is there ever a reason to use HttpResponse over render? If so, what would be the use case and advantage of doing so? Thank you.

like image 887
David542 Avatar asked Sep 04 '11 20:09

David542


2 Answers

render is used to for what the name already indicates: to render a template file (mostly HTML, but could be any format). render is basically a simple wrapper around a HttpResponse which renders a template, though as said in the previous answer, you can use HttpResponse to return other things as well in the response, not just rendering templates.

like image 121
Torsten Engelbrecht Avatar answered Sep 19 '22 18:09

Torsten Engelbrecht


Sure, say you're making an AJAX call and want to return a JSON object:

return HttpResponse(jsonObj, mimetype='application/json')

The accepted answer in the original question alluded to this method.

like image 30
Abid A Avatar answered Sep 22 '22 18:09

Abid A