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 Test Time Django TDD

Dmitry Karamin
Dmitry Karamin
10,099 Points

Can't pass one assert in test

i've passed all tests with the exception of last line in this test:

class ViewTests(TestCase):
    def setUp(self):
        self.performer = Performer.objects.create(name='Craig Dennis')
        self.song = Song.objects.create(
            title='I Wanna Be Sedated',
            artist='The Ramones',
            length=149,
            performer=self.performer)
    def test_performer_detail_view(self):
        '''The performer_detail view should:
           * return a 200
           * have self.performer in the context
           * use the songs/performer_detail.html template
           * show the string version of self.song in the template
        '''
        resp = self.client.get(reverse('songs:performer',
                                       kwargs={'pk': self.performer.pk}))
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.context['performer'], self.performer)
        self.assertTemplateUsed(resp, 'songs/performer_detail.html')
        self.assertContains(resp, str(self.song))

Can't pass last line although i pass all tests in Eclipse

songs/models.py
from django.db import models

# Create your models here.


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

    def __str__(self):
        return self.name


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

    class Meta:
        ordering = ['artist', ]

    def __str__(self):
        return '{} by {}'.format(self.title, self.artist)
songs/views.py
from django.shortcuts import render, get_object_or_404

from .models import Song, Performer

# Create your views here.


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


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

def performer_detail(request, pk):
    performer = get_object_or_404(Performer, pk=pk)
    song = performer.song_set.get(pk=pk)
    context = {
        'performer': performer,
        'song': song
    }
    return render(request, 'songs/performer_detail.html', context)
songs/templates/songs/performer_detail.html
{% extends 'base.html' %}

{% block title %}{{ performer }}{% endblock %}

{% block content %}
<h2>{{ performer }}</h2>
{% endblock %}

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

You are very close. In the view performer_detail you pass song in the context but do not include a {{ song }} in the performer_detail.html template.

Dmitry Karamin
Dmitry Karamin
10,099 Points

i thought that i can't change templates...

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

Sorry if that wasn't clear. If the challenge allows you to paste code as part of the solution then changing the file is usually fair game.