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

bryonlarrance
bryonlarrance
16,414 Points

Article.len() syntax

def article_list(request): output = Article.len() return HttpResponse('There are ' + output + ' articles.')

I think I have an error in my syntax, but I am not sure. Any hints??

articles/views.py
from django.http import HttpResponse
from .models import Article
# Write your views here

def article_list(request):
    output = Article.len()
    return HttpResponse('There are ' + output + ' articles.')

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

You are very close. Remember, to get all of the Article instances in Django you need to use the model manager:

Article.objects.all()

This returns a queryset of all results. you can then use the len() function (not a method!) on this queryset to get the number of objects.

Also, remember to convert your length result to a string before using concatenation: str(output). Alternatively, you could use a .format() statement that auto converts output to a string for you!

Post back if you're still stuck. Good Luck!!!