Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Django Basics Model Administration First app view

Code challenge with django

Can anyone advise me on this, why is it a bummer? Thanks for the help so far

articles/views.py
from django.http import HttpResponse

# Write your views here
from .models import Article

def article_list(request):
    articles = Article.objects.all()
    article_len = len(articles)
    return HttpResponse("There are " + article_len + "articles")

Ok, I think this might answer your question.

  • If the variable article_len is populated by an int type then Python will throw you a [ TypeError: can only concatenate str (not "int") to str ], as you would be trying to do just that.
def article_list(request):
    articles = Article.objects.all() # quering all Article Objects
    article_len = len(articles) # getting an integer I assume.
    # if the variable article_len is populated by an int type then Python will throw you
    # a TypeError: can only concatenate str (not "int") to str.
    return HttpResponse("There are " + article_len + "articles") # here is the type error.

You can do what has already been suggested -> using .format() as format converts to string.

return HttpResponse("There are {} articles.".format(len(articles)))

You can also do the conversion from int to string while concatenating.

return HttpResponse("There are " + str(article_len) + "articles.")

Or you can do :

return HttpResponse("There are s% articles." % ( len( articles ) ) )

2 Answers

Cheo R
Cheo R
37,150 Points

Not sure why (maybe the regex they use to check answer), but I got it to pass keeping your code except, returning the string with format.

HttpResponse("There are {} articles".format(article_len))

using format worked, thanks for all your efforts. PHP would have allowed it though!