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 trialGeoffrey Powell
15,358 PointsDjango Basics > First App View > Challenge 2
Just wondering why my code here isn't passing? I've spent two hours going back over the video, looking in the Django documentation, etc. Not sure what I'm missing.
from django.http import HttpResponse
from .models import Article
# Write your views here
def article_list(request):
articles = Article.objects.all()
output = len(articles)
return HttpResponse("There are" + output + " articles")
4 Answers
Geoffrey Powell
15,358 PointsSolved by using:
from django.http import HttpResponse
from .models import Article
# Write your views here
def article_list(request):
articles = Article.objects.all()
output = ' '.join(['There are',str(len(articles)),'articles.'])
return HttpResponse(output)
Daniel Jeffery
Courses Plus Student 1,190 PointsCan't you do that kind of string concatenation (as you would do in say JavaScript) in Python? What's the deal with that?
The way you tried to do it, is the way I tried to to do it, and it looks much nicer to me :P
Daniel Jeffery
Courses Plus Student 1,190 PointsAh!
I figured out what you originally had incorrect.
The output variable was holding a number, you need to do str(output), so that it is a string. You can't concatenate a number to a string :)
ursaminor
11,271 PointsThanks! Forgot that you can't concatenate strings and numbers. But join() is not necessary. Here's a simpler and more readable way:
def article_list(request):
num_articles = len(Article.objects.all())
return HttpResponse("There are " + str(num_articles) + " articles.")