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

First app view task 2

im not quite sure what im supose to do for this challenge

articles/views.py
from django.http import HttpResponse
from .models import Article
# Write your views here
def article_list(request):

1 Answer

Hi Emily

Here's the code that passed for me. I will walk you through it down below. I actually use f-string but not quite sure whether you know this so I'm going to put two solutions here which both work:

Using .format:

from django.http import HttpResponse

from .models import Article


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

Using f-string:

from django.http import HttpResponse

from .models import Article


def article_list(request):
    articles = len(Article.objects.all())
    output = f'There are {articles} articles.'
    return HttpResponse(output)
  • first, we need to import all the necessary stuff
  • second, we create a view which you did correctly - def article_list(request):
  • on the next line we need to select all Article instances. We can do it like this: Article.objects.all(). We also need to assign it to a variable, in this case articles. In addition, we need to find the length of the Article queryset so the easiest way to do this is just to put it in the Python's length function len()
  • on the next line we assign the final answer to a output variable
  • lastly, we return the HttpResponse

I guess for the last two lines you can also do this:

return HttpResponse('There are {} articles.'.format(articles))

But I personally like to assign different stuff to variables. In this case the return line looks more readable.

I hope this helps!