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

Cirrus Streamer
Cirrus Streamer
4,351 Points

I cant get it to pass, any ideas?

Any help appreciated.

songs/models.py
from django.db import models

# Write your models here


class Performer(models.Model):
  name = models.TextField(default='')

  def __str__(self):
    return self.name


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

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

from .models import Song, 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})

def performer_detail(request, pk):
    performer = get_object_or_404(Performer, pk=pk)
    songs = Song.objects.filter(performer=performer)
    return render(request, 'songs/performer_detail.html', {'Performer': performer, 'Songs':songs})
songs/templates/songs/performer_detail.html
{# some code changes follow #}
{% extends 'base.html' %}

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

{% block content %}
{# removed link #}
{# <h2>{{ performer }}</h2> #}

{# Added loop to list songs #}
{% for song in songs %}
{{ song }}
{% endfor %}
{% endblock %}

3 Answers

Ok here is what i did. See below

models.py

from django.db import models

# Write your models here

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

  def __str__(self):
    return self.name


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


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

views.py

from django.shortcuts import render,get_object_or_404

from . models import Performer,Song

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})


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

performer_detail.html

{% extends 'base.html' %}

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

{% block content %}
<h2>{{ performer }}</h2>
{% for song in performer.song_set.all %}
    {{ song.title }} by {{ song.artist }}
{% endfor %}
{% endblock %}

Hi Cirrus

In the performer_detail view you don't need to pass the songs in its context, i also noticed you were passing performer with 'p' in uppercase while using it as a lowercase in the template. To list all the songs for a performer, you can use the _set.all() query set. This worked for me see below

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


{# Added loop to list songs #}
{% for song in performer.song_set.all %}
        {{ song.title }} by  {{ song.artist }}
{% endfor %}
Cirrus Streamer
Cirrus Streamer
4,351 Points

No still not passing - this is a beastly task!!!

Anymore ideas anyone please, I cant think straight on this one anymore.