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 trial

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!