How to add new line inside a <p> tag in html when using Django to pass data?

Viewed 47

I have model of Players in Django which looks like this:

class Player(models.Model):
    name = models.CharField(max_length=200, null=False, blank=False, default='')
    description = models.TextField(max_length=1000, null=True, blank=True)

I pass the Player objects in the context:

def playerView(request):
    players = Player.objects.all()
    context = {'players':players}
    return render(request, 'base/players.html', context)

Now, I render the description in p tag of html using below code:

players.html

{% for player in players %}
<p>{{player.name}}</p>
<p>{{player.description}}</p>
{% endfor %}

But when I try to add description with newlines in it like:

This is line one.
This is line two.

It renders data like this:

This is line one.This is line two.

I tried adding &#13 &#10 which I found in one of the answers is a new line character, but it doesn't work. I also tried adding \n and also tried adding br tag, but that also didn't work. Please help me with a solution.

1 Answers

Based on django docs I think linebreaks template tag can help you

<p>{{player.description|linebreaks}}</p>

Or use <pre

Related