How to make HttpResponse print the new line?
By default HttpResponse
has "text/html" as content-type, and html ignores newlines chars (or more exactly treats them as whitespaces).
If you want a text/html response content, you'll have to replace newlines by the appropriate html markup (either using <br>
instead of newlines or wrapping each line in a <div>
or <p>
etc). You'll also need to wrap the whole thing into a proper html document, IOW adding the parent <html>
and <body>
tags around your content etc.
Else if a plain text content type is ok, just set the content-type header on your response:
return HttpResponse(output, content_type="text/plain")
so the browser knows to interpret and render it correctly.
As a side note: the way you're using both the orm and str.join()
is vastly inefficient.
First, str.join()
takes any iterable (as long as all the items are strings) so you don't need to build a list
- you could just use a generator expression ie (notice the lack of brackets around the expression):
output = "\n".join(each_review.review_text for each_review in review)
But if you're only interested in a single field from the model, loading the whole fields and building a model instance is a complete waste of resources both at the database and python code levels. The solution here is to use a QuerySet.value_list()
instead (here with the flat
flag set to True since we only want a single field):
output = "\n".join(Review.objects.values_list("review_text", flat=True))
you get a html so you should to use <br>
output = "<br>".join([each_review.review_text for each_review in review])
or <pre>
output = "\n".join([each_review.review_text for each_review in review])
output = "<pre>{}</pre>".format(output)