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 trialAbe Daniels
Courses Plus Student 2,781 PointsNeed an extra set of eyes to look this over.
I am trying to create a new view and the error I am getting back is kinda vague.
Can someone be my bug splatter?
#Now, create a view named article_list that selects all Article instances and returns an HttpResponse like "There are 5 articles."
#Be sure to use the len() of the Article queryset to get the number of articles.
from django.http import HttpResponse
from .models import Article
def article_list(request):
s = Article.objects.all()
output = 'There are ' + len(s) + ' articles'
return HttpResponse(output)
3 Answers
Ryan Clodfelter
10,247 PointsDjango is probably complaining because you are trying to implicitly cast an int as a string.
You can use .format to fix your issue like so...
from django.http import HttpResponse
# Write your views here
from .models import Article
def article_list(request):
s = Article.objects.all()
output = 'There are {} articles'.format(len(s))
return HttpResponse(output)
oxanaox
16,295 PointsRyan, your solution does not work. Any ideas?
Ben Ackles
27,551 PointsRyan's solution works fine. The only problem is the string is missing a period (.) at the end. It's supposed to read "There are 5 articles."
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsCorrect, if updated with Ben Ackles comment below: "missing a period (.)"