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

Copy and paste your models, views, and performer template from Workspaces into the correct files below.

Why is this code not passing?

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

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


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

  def __str__(self):
    return self.name
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.name }}{% endblock %}

{% block content %}
<h2><a href="{% url 'songs:detail' pk=performer.song.pk %}">{{ performer.song.title }}</a></h2>
{% endblock %}

5 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Hi Ginny, I compared your code to a known passing version and found these differences. Making these changes passes the challenge:

# in views.py file:
def performer_detail(request, pk):
    performer = get_object_or_404(Performer, pk=pk)
    # Add missing songs
    songs = Song.objects.filter(performer=performer)
    # Add songs to template context
    return render(request, 'songs/performer_detail.html', {'performer': performer, 'songs': songs})
{# in performer_detail.html template, changes marked by comments such as this #}
{% extends 'base.html' %}

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

{% block content %}
  {# removed link #}
  {#<h2><a href="{% url 'songs:detail' pk=performer.song.pk %}">{{ performer.song.title }}</a></h2>#}
  <h2>{{ performer }}</h2>#}
  {# Add loop to list songs #}
  {% for song in songs %}
   {{ song }}
  {% endfor %}
{% endblock %}

I've tried making the same changes and can't get it to pass...

Seems like it should.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

cj thompson: I suggest you create a new forum post and include your code.

nicole lumpkin
nicole lumpkin
Courses Plus Student 5,328 Points

Chris Freeman I am so confused. If views.performer_detail passes performer to performer_detail.html, how in the world are you able to pull off your for song in songs loop?

nicole lumpkin
nicole lumpkin
Courses Plus Student 5,328 Points

What I really want to know Chris Freeman is why my version of performer_detail.html fails the last assertion in the last test in tests.py ?

{% extends 'base.html' %}

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

{% block content %}
<h2>{{ performer }}</h2>
{{ performer.song_set.all|linebreaks }}
{% endblock %}

UPDATE: Guess what ya'll! With a little help from a developer friend of mine, we figured it out! It turns out that Django was turning my greater than/less than characters('<title> by <artist>' ), to '&gt;' and '&lt;' respectively. I'm a noob, but he explained that those characters ('>' and '<') essentially were being escaped and transformed into the strings in bold. So when self.assertContains(resp, str(self.song)) went looking for '<I Wanna Be Sedated> by <The Ramones>', it instead found '&gt;I Wanna Be Sedated&lt;by&gt;The Ramones&lt;' BUT, if you use the filter safe it prevents this! Check out my passing performer_detail.html!!!

{% extends 'base.html' %}

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

{% block content %}
<h2>{{ performer }}</h2>
{{ performer.song_set.all|safe|linebreaks }}
{% endblock %}

This also passes!!!

{% extends 'base.html' %}

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

{% block content %}
<h2>{{ performer }}</h2>
{% for song in performer.song_set.all %}
{{ song|safe }}
{% endblock %}

WHOOP WHOOP!!

not a fan of this challenge.

Samuel Ferree
Samuel Ferree
31,722 Points

So this whole time the challenges have been working with Articles, and writers. In the videos, Kenneth is making a learning site app, and this challenge comes out of nowhere with "Copy your songs and artists from workspaces"

What gives? I have literally no idea what the challeneg is talking about.

Cirrus Streamer
Cirrus Streamer
4,351 Points

The challenge is an entirely new app, with new scenario and objects as a pretend first project. I still cant get it to pass.

Max Hirsh
Max Hirsh
16,773 Points

If you run the project in Workspaces and it passes all the tests, it could be a timeout error. I ran into this issue myself, and the way I solved the problem was by attempting to submit it multiple times/at different times of day. You could even try using a different browser and see if that helps. The issue seems to be that the project is fairly large and sometimes takes longer than the time limit to evaluate.

Chris, I think I realized the error...I wasn't using Workspaces, I was working along in Django. Thanks for the response. I'll try it again in a few days...

EDIT:

I meant I was following along in PyCharm. Jeez. Long day. But I think my problem is that I need to use workspaces. For whatever reason.

Thanks again for responding so quickly.