Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Django Basics Model Administration First app view

Vince Varga
Vince Varga
15,282 Points

Length of queryset in a Django response

I couldn't get this test to work. First, I tried this.

from django.http import HttpResponse

from .models import Article

def article_list():
    return HttpResponse('There are {count} articles.'.format(count=len(Article.objects.all())))

I thought about 'There are no articles.' and 'There is 1 article.', but guessed that wouldn't be necessary at such a basic level, or there would be some hints about that in the question.

from django.http import HttpResponse

from .models import Article

def article_list():
    articles = Article.objects.all()
    response_str = None
    if len(articles) > 1:
        response_str = 'There are {count} articles.'.format(count=len(articles))
    else if len(articles) is 1:
        response_str = 'There is 1 article.'
    else:
        response_str = 'There are no articles.'
    return HttpResponse(response_str)

1 Answer

Krasimir Georgiev
Krasimir Georgiev
19,146 Points

Your code is correct. You don't need all those extra cases. It's just that every view needs a parameter in which the request object gets passed in.

def article_list(request):