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 trialShana HT
3,292 PointsTDD Django Last Challenge - How to Interpret this Test Error
FAIL: test_performer_detail_view (songs.tests.ViewTests)
The performer_detail view should:
Traceback (most recent call last):
File "/home/treehouse/workspace/karaoke/songs/tests.py", line 74, in *test_performer_detail_view
self.assertContains(resp, str(self.song)) *
File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/django/test/testcases.py", line 403, in assertContains
msg_prefix + "Couldn't find %s in response" % text_repr)
AssertionError: False is not true : Couldn't find 'I Wanna Be Sedated by The Ramones' in response
in views.py
def performer_detail(request, pk):
performer = get_object_or_404(Performer, pk=pk)
performer.song_set.all()
return render(request,'songs/performer_detail.html',{'performer':performer})
[MOD: added ```python formatting -cf]
6 Answers
Shana HT
3,292 PointsThis test is tripping it up.
But I think the test is wrong, I'm just not sure why. Project runs as expected, and all other tests pass. Why is this test not passing?
def test_performer_detail_view(self):
'''The performer_detail view should:
* return a 200
* have self.performer in the context
* use the songs/performer_detail.html template
* show the string version of self.song in the template
'''
resp = self.client.get(reverse('songs:performer',
kwargs={'pk': self.performer.pk}))
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.context['performer'], self.performer)
self.assertTemplateUsed(resp, 'songs/performer_detail.html')
#this line is causing the test to fail
self.assertContains(resp, str(self.song))
Kenneth Love
Treehouse Guest TeacherWhat's in your template? What's the __str__
method for the Song
model?
Shana HT
3,292 PointsWhat's in your template?
This is performer.html
{% extends 'base.html' %}
{% block title %}{{ performer.name }}{% endblock %}
{% block content %}
<h2>{{ performer }}</h2>
{% for song in songs %}
{{ song }}
{% endfor %}
{% endblock %}
What's the str method for the Song model?
def __str__(self):
return '{} by {}'.format(self.title, self.artist)
peterbristow
10,403 PointsThe str method in the Song model is correct, line 5 of performer.html needs fixing :)
Nathan Magyar
11,332 PointsThe other important part to my solution that passed the last test was making sure I was adding the performer's song list to the context, not just using performer.song_set.all()
in the performer_detail.html template:
def performer_detail(request, pk):
performer = get_object_or_404(Performer, pk=pk)
songs = performer.song_set.all()
return render(request, 'songs/performer_detail.html', {'performer': performer, 'songs': songs})
Alex Staenke
7,324 PointsThe test looks exactly for this format "<title> by <artist>" Including the < and >.
The return from your str method should be something like "<Thriller> by <Michael Jackson>"
If you have that keep in mind that "<" and ">" are special characters in HTML and are therefor autoescaped by Django templates. #thistookmealongtimetofigureitout
Nathan Magyar
11,332 PointsUnfortunately Alex Staenke's suggestion is incorrect. I passed this final test once my str method looked like so:
def __str__(self):
return '{} by {}'.format(self.title, self.artist)