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

Vince Varga
Vince Varga
15,338 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):