Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Basics Model Administration First app view

David Seitz Jr
David Seitz Jr
3,363 Points

How do I return .len() in the HttpResponse()?

I just have no idea how to return the length in the HttpResponse method. This may have more to do with my unfamiliarity with Python than anything else...

articles/views.py
from django.http import HttpResponse
from .models import Article

def article_list(request):
    length = Article.objects.all().len()
    return HttpResponse('(%d)' % length)

1 Answer

There's just a few things that I'm noting. len is not a method of the QuerySet returned by Article.objects.all(). You could use the method called count, but they are asking you to use len which is a built in function. The other thing is that you aren't returning the string that the challenge wants you to return. That is an easy fix.

from django.http import HttpResponse
from .models import Article

def article_list(request):
    length = len(Article.objects.all())  # Call len as a function and not as a method
    return HttpResponse('There are %d articles.' % length)  # Return the proper string

Those two things should complete the challenge for you.

David Seitz Jr
David Seitz Jr
3,363 Points

Beautiful! Thank you for helping me through that. ??