Django text formatting in template

Viewed 1108

I am trying to format text rendered from database in django template.

#models.py

class Question(models.Model):
    quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE, related_name='questions')
    text = models.TextField('Question', max_length=1500)
    def __str__(self):
        return self.text

the text what i am entering in the text field is like

a=50
b=30
c=a*b

Now When I am calling this value in the template like.

#template.html

<p>{{ question.text }}</p>

the text is printing like below in the template

a=50 b=30 c=a*b

I want the text to be formatted with line breaks while calling the values in template

a=50
b=30
c=a*b
2 Answers

try this in tour template, it will do the trick for you

#template.html

<p>{{ question.text|linebreaks }}</p>

try this it will work!

{% for q in question %}
    <p>{{ q.text }}</p>
{% endfor %}
Related