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 trialahmedagour
Courses Plus Student 216 PointsI am trying to get the length of the Article objects list from the django models. I am not sure what I am doing wrong
I am trying to get the length of the Article objects list from the django models. I am not sure what I am doing wrong. I have put a place holder. not sure what is my problem.
from django.http import HttpResponse
# Write your views here
from .models import Article
def article_list(response):
x= Article.objects.len()
return HttpResponse("There are %s articles." %x)
1 Answer
diogorferreira
19,363 PointsHey, len() isn't an attribute of Article.objects, so you wouldn't be able to call it by using x= Article.objects.len()
You need to pass all of the Article objects into the len() function like so: x= len(Article.objects.all())
This would be the completed code:
from django.http import HttpResponse
# Write your views here
from .models import Article
def article_list(response):
x= len(Article.objects.all())
return HttpResponse("There are %s articles." %x)