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

Milos Trifunovic
Milos Trifunovic
2,801 Points

Passing all tests except 'test_performer_detail_view'

I have no clue what to do and what is the problem with my code. I guess the problem is in performer_detail.html, but not sure. If you can take a look.

from django.db import models

# Write your models here

class Performer(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return '{}'.format(self.name)

class Song(models.Model):
    title = models.CharField(max_length=255)
    length = models.IntegerField(default=0)
    artist = models.CharField(max_length=255)
    performer = models.ForeignKey('Performer')

    def __str__(self):
        return  '{} by {}'.format(self.title, self.artist)
from django.shortcuts import get_object_or_404, render
from .models import Song, Performer
from django.http import HttpResponse

def performer_detail(request, pk):
  performer = get_object_or_404(Performer, pk=pk)
  return render(request, 'songs/performer_detail.html', {'performer': performer})

def song_list(request):
  songs = Song.objects.all()
  return render(request, 'songs/song_list.html', {'songs': songs})

def song_detail(request, pk):
  song = get_object_or_404(Song, pk=pk)
  return render(request, 'songs/song_detail.html', {'song': song})
{% extends 'base.html' %}

{% block title %}{{ performer.name }}{% endblock %}

{% block content %}
<section>
    {% for song in performer.song_set.all() %}
        <h3>{{ song.title }}</h3>
    {% endfor %}
</section>
{% endblock %}

4 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

What message is coming back from the tests when you run them in Workspaces?

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

So, I don't want to give this all away. Look at the Song model's __str__ method. When you print a Song instance, what comes out? And what's the test looking for? And what are you printing in the template?

Milos Trifunovic
Milos Trifunovic
2,801 Points

Here is what's printed in the console when I run the tests.

The performer_detail view should:                                                                
----------------------------------------------------------------------                           
Traceback (most recent call last):                                                               
  File "/home/treehouse/workspace/karaoke/songs/tests.py", line 74, in test_performer_detail_view
    self.assertContains(resp, str(self.song))                                                    
  File "/usr/local/pyenv/versions/3.4.1/lib/python3.4/site-packages/django/test/testcases.py", li
ne 358, in assertContains                                                                        
    msg_prefix + "Couldn't find %s in response" % text_repr)                                     
AssertionError: False is not true : Couldn't find 'I Wanna Be Sedated by The Ramones' in response

----------------------------------------------------------------------                           
Ran 5 tests in 0.074s                                                                            

FAILED (failures=1)                                                                              
Destroying test database for alias 'default'...      

I tried to manually create performer and some songs to test performer_details.html but it still doesn't bring up song names.

Milos Trifunovic
Milos Trifunovic
2,801 Points

After testing and experimenting for hours (can't say it wasn't useful as I learned a lot), I found the error was so simple that I don;t know how I overlooked it!

The problem was I didn't edit the performer_details.html file in songs/templates/songs folder but instead, I created and edited one in the outer templates folder. So simple, yet, you focus on other stuff and don't see it.

PS And there was one small error in template, I won't spoil for others what was it, it's also simple.