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 trialBruce Whealton
3,619 PointsWhat am I doing wrong in this Django Basics task - creating a view to display the count of articles?
I wrote the following code: from django.http import HttpResponse
Write your views here
from .models import Article
def article_list(): articles = Article.objects.all() count = str(len(articles)) output = "There are %s articles".format(count) return HttpResponse(output)
The task asks me to to create a view that will show "There are count articles." Maybe I need a %d and not to convert the count, which is a number, to a string. This is in the Django Basics course. Bruce
from django.http import HttpResponse
# Write your views here
from .models import Article
def article_list():
articles = Article.objects.all()
count = str(len(articles))
output = "There are %s articles".format(count)
return HttpResponse(output)
2 Answers
Pedro Pólvora
14,086 PointsI believe the string should be like this, (note that we're using python 3)
output = "There are {} articles".format(count)
And also, your view should take the request as an argument
def article_list(request):
.....
Daniel Jeffery
Courses Plus Student 26,985 PointsGot it to work by adding request to the view and changing %s to {} as Pedro said
Amy Plant
5,591 PointsI got it to work doing that too when I test it, but the course keeps telling me: Bummer! Didn't get the right output.
from django.http import HttpResponse
from.models import Article
# Write your views here
def article_list(request):
articles = Articles.objects.all()
number_of_articles = str(len(articles))
output = "There are {} articles.".format(number_of_articles)
return HttpResponse(output)