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

Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

Please someone can help me out

Please someone explain what is missing. I am sttucked here for a while. I can't pass only one test function from the project and I don't know how to solve it. This is the test what i can't pass:

self.assertContains(resp, str(self.song))
songs/models.py
from django.db import models



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)
    length = models.IntegerField(default=0)
    performer = models.ForeignKey(Performer)


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

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)
    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><a href="{% url 'songs:performer' pk=performer.pk %}">{{ performer }}</a></h2>
  <ol>
    {% for song in songs %}
    <li>{{ song }}</li>   
    {% endfor %}
  </ol>
{% endblock %}

2 Answers

Krasimir Georgiev
Krasimir Georgiev
19,146 Points

First off performer_detail.html doesn't know about songs since you haven't passed it in the context of the performer_detail view. One way would be to pass the songs in the context but the other more convenient way is to get all the songs from the performer you have already passed in like so:

{% for song in performer.song_set.all %}

This works because every model has a set of all the other models that reference him in their ForeignKey field.

The other thing that really bugged me as well for a long time is that the title and artist shouldn't actually be surrounded by <> so in the models the string representation of song should be:

def __str__(self):
        return '{} by {}'.format(self.title, self.artist)
Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

Hi Krasimir, I can't thank you enough in words, i was sttucked here more tha a week and i didnt have nobody to ask.

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

about this, in the quiz it showes like this with <> thats why i putted those. I dont want to complain about the quiz but sometimes there are not explicit enough and people who doesent understand 100% english will understand it wrong, like me. Thank you very mush!