본문 바로가기

Django

django / template <br> 줄바꿈 방법

 

database에 다음과 같이 입력되어 있는 글을 가지고 template으로 나오면

This is the text

This is on a new line with a space in between

 

 

다음과 같이 줄바꿈 없이 글이 써진다. 줄바꿈이 필요해 😥

This is the text This is on a new line with a space in between

 

2가지방법 

 

1. linbreaks / linebreaksbr filter 사용하기

{{text|linebreaks}}

 

2. <pre>...</pre> 사용하기

<pre>{{ text }}</pre>

 

두개 차이점이라면,

<pre> 사용시 줄바꿈이 2줄인 경우 그대로 나오는데, 1번의 경우 1번만 되서 나온다. 

 

 

참고

https://stackoverflow.com/questions/22538454/how-to-output-text-from-database-with-line-breaks-in-a-django-template

 

 

반응형