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

Marcos L贸pez
Marcos L贸pez
7,493 Points

why is my code wrong?

If I get the array of Article objects, is just format the string output with the length of that array

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

def article_list(request):
  articles = Article.objects.all()
  output = "There are {0} articles".format(articles.len())
  return HttpResponse(output)
Marcos L贸pez
Marcos L贸pez
7,493 Points

Sorry, I put articles.len() and the right way is len(articles), maybe I was confused with other programming languages that is the way to get the len of an array

1 Answer

Steven Parker
Steven Parker
229,920 Points

Looks like you realized you needed len(array) on your own. :+1:

I'm not aware of a language that uses a method for length. I know in JavaScript it's a property (array.length).

Ross Trimble
Ross Trimble
3,598 Points

C++ uses the length method for strings.


returns the number of characters in a string. Most of the other standard containers in C++ (arrays, lists, vectors, also strings as well) use the size method my_array.size() for the number of items inside them.