[Fixed]-Django how to use multiple lines with return HttpResponse

1👍

Learn about Python and Django a little more. HttpResponse is a quick and dirty way that output is done in views. You can learn about how use Templates for this, you can read about this in:

https://docs.djangoproject.com/en/1.9/intro/tutorial03/

and use Templates for your process.

0👍

Just use <br/> where you want the break line. When browser finds the <br/> changes line.

n = random.randint(1, 2)  # returns a random integer
if n == 1:
    query = "The number is one."
else:
    query = "The number isn't one."
return HttpResponse('Random Number Generator <br/> The random number is: ') + str(n) + "<br/>" + query)
👤Rafael

0👍

I think the best solution would be to set content_type="text/plain":

n = random.randint(1, 2)  # returns a random integer
if n == 1:
    query = "The number is One."
else:
    query = "The number Isn't One."
output_string = "Randomly generated number: \n The random number is: " + str(n) + "\n" + query
return HttpResponse(output_string, content_type="text/plain")

The default content type of HttpResponse is text/html (docs), so using <br> would work. However, this essentially creates a non-valid HTML page. Thus, for such simple output, I think text/plain is the best choice.

👤Travis

Leave a comment