I am using Django for development, retrieving some text containing a newline character from the database. However, when I render it to a template using a template tag it doesn’t show the newline character.
What is the problem?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You have to remember that your templates are producing HTML. In HTML, a newline character is just another white space, it doesn’t mean to put the following text onto a new line. There are a number of ways to force new lines in HTML.
You can wrap your text with a
<pre>tag so that HTML will understand that it is preformatted:You can use Django filters to convert your plain text newlines into HTML.
linebreaksturns single newlines into<br>tags, and double newlines into<p>tags.linebreaksbrjust turns newlines into<br>tags:You can experiment with these to see which you like better.
Failing that, you can use string manipulation in your view to convert your plain text into HTML in a way that suits you better. And if you want to get really advanced, you can write your own filter that converts the way you like, and use it throughout your templates.