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 Congratulations!

Michael Nwani
Michael Nwani
4,669 Points

Can't pass the last challenge

Regarding Performer and Song, I'm not sure what I'm missing.

My models.py:

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 self.title + " " + self.artist

My views.py:

from django.shortcuts import get_object_or_404, render

from .models import Performer, Song


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

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

with performer_detail.html, I've tried many variations, but here's the last one I tried:

{% extends 'base.html' %}

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

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

  {% for song in performer.song_set.all %}
      {% if song.performer.pk == performer.pk: %}
        {{ song }}
      {% endif%}
  {% endfor %}
{% endblock %}

Previously I just left out the if statement and just tried {{song}}. Also, the Performer model isn't supposed to have a foreign key back to Song is it (Song is the only one with a foreign key to Performer)?

Thanks!

4 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

The string version of your Song should be title by artist, IIRC. Not sure why yours isn't, actually.

You don't need the if.

And, no, the performer doesn't have a link back to the song except through song_set.

Michael Nwani
Michael Nwani
4,669 Points

In the tests.py, the comment says " '''String version of Song should contain the title and artist''' ", so I just had Song's string version be self.title + " " + self.artist

Even after trying self.title + " by " + self.artist and removing the if block in performer_detail, I'm still getting a failure with no error hint.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Yeah, you're right. I misremembered that.

What message do you get when you run the tests in Workspaces or on your local machine?

Christopher Shaw
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Shaw
Python Web Development Techdegree Graduate 58,248 Points

To get it to pass, my performer_detail.html looks more messy than I would like.

{% 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 %}

I expected {{ song }} to work, but kept giving the fail: Couldn't find '<I Wanna Be Sedated> by <The Ramones>' in response

I have included my Song model below, to show that it is returning the correct response.

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)
Chris Komaroff
Chris Komaroff
Courses Plus Student 14,198 Points

Thank you! Hopefully they fix this error soon. To repeat: duration field is really length, be advised.

Michael Nwani
Michael Nwani
4,669 Points

Well I never really tried it in the Workspaces (did all the coding in a local directory), but I just fixed it lol. I had a few typo's:

1) changed "{'songs' : song})" to "{'songs' : songs})" under song_list

2) forgot to import models from django.db in models.py

Also fyi the "self.title + " " + self.artist" ran correctly

Thanks!

John Caraballo
John Caraballo
19,943 Points

I have this EXACT code and 4 tests are failing haha. Mind you I do not have the typos you mentioned but yeah everything else is pretty much the same. I'm doing it in workspaces and the part that REALLY bugs me is the str of Song which is set to the correct string but STILL fails haha

from django.db import models

# Write your models here

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

  def __str__(self):
    return self.title + " " + self.artist


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

  def __str__(self):
    return self.name

EDIT:

My error was the length attribute(column) I had it set to duration. That did it! haha

David Lin
David Lin
35,864 Points

Yeah, the specs for the song model in the README.txt specified "duration", but in the tests, it's "length". Tripped me up, too.