0

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

2
  • 2
    Please edit your question, and give a bit more information on what you are trying to achieve, what steps you took and particularly what "run is True but don't run ',\n'" is supposed to mean. Commented Jun 29, 2018 at 15:36
  • Also, it's better to post your results as text as well, not images. Commented Jun 29, 2018 at 15:43

1 Answer 1

1

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])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.