Zeth
Zeth

Reputation: 2648

Django / Python make newline with '\n'

I'm quite new to using Django, so please be gentle, if this is a stupid question.

I'm trying to play with some stock-information in Python/Django. I'm getting the stock-rates from Yahoo Finance (with urllib). Whenever I get some stock-information, then I will get it like this (yep, it's one long string):

[b'Date,Open,High,Low,Close,Volume,Adj Close\n2014-12-01,26.16,26.22,25.85,26.02,48967100,25.54\n2014-11-28,26.80,26.90,26.44,26.49,31185200,26.00\n2014-11-26,26.89,26.97,26.78,26.87,19289700,26.38\n2014-11-25,27.01,27.03,26.84,26.86,28028000,26.37\n']

I then send it to a template with Django, using

to_be_returned = RequestContext(request, [[DICTIONARY_WITH_VARIABLE]]) 

and then

return HttpResponse(template.render(to_be_returned)

Is there a way, to get Django to beautify the long string for me? So I don't have to manipulate the string and replace all \n with <br />, in order for me to be able to read it easier? And what does the [b' ... '] mean (that contains the entire result?

Thanks for your assistance.

Upvotes: 1

Views: 4246

Answers (3)

Harsimran Kaur
Harsimran Kaur

Reputation: 11

You can add html tag to the strings that you write in the response

For example :

response2.write('</br></br>List of Courses : ')

Upvotes: 1

Serge K.
Serge K.

Reputation: 348

You do not attached a template. Wrap out tag with

<pre></pre>

Upvotes: 1

catavaran
catavaran

Reputation: 45595

Use the linebreaksbr template filter:

{{ my_string_with_n|linebreaksbr }}

b'...' is a python3 byte string literal.

Upvotes: 8

Related Questions