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

i am stuck

last challenge

songs/models.py
from django.db import models

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

    def __str__(self):
        return '{} by {}'.format(self.title, self.artist)


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

    def __str__(self):
        return self.name
songs/views.py
from django.shortcuts import render
from django.views import generic

from .models import Song, Performer


class SongListView(generic.ListView):
    model = Song
    context_object_name = 'songs'
    template_name = 'songs/song_list.html'


class PerformerDetailView(generic.DetailView):
    model = Performer
    context_object_name = 'performer'
    template_name = 'songs/performer_detail.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['performer'] = Performer.objects.get(pk=self.kwargs['pk'])
        return context


class SongDetailView(generic.DetailView):
    model = Song
    context_object_name = 'song'
    template_name = 'songs/song_detail.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['song'] = Song.objects.get(pk=self.kwargs['pk'])
        return context

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

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

{% block content %}
<h2>{{ performer }}</h2>
<ul>
    {% for song in performer.song_set.all %}
        <li>{{ song }}</li>
    {% endfor %}
</ul>
{% endblock %}