Armin
Armin

Reputation: 1

Browser doesn't display newlines when using "\n".join() in Django

My code in Django is:

from django.shortcuts import render
from django.http import HttpResponse
from .models import Post

# Create your views here.

def index(request):
    list_posts = Post.objects.order_by('-publish')[:5]
    output = ',\n'.join([p.slug for p in list_posts])
    return HttpResponse(output)

The view runs without errors, but I do not see the newlines in the browser.

enter image description here

Upvotes: 0

Views: 247

Answers (1)

Alasdair
Alasdair

Reputation: 309109

If you look at the page source in your browser, it will contain \n, but your browser will display these as regular spaces.

You can use a <br> tag if you want your browser to display each slug on a new line.

output = '<br>'.join([p.slug for p in list_posts])

Upvotes: 1

Related Questions