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 trialDmitry Karamin
10,099 PointsCan't pass one assert in test
i've passed all tests with the exception of last line in this test:
class ViewTests(TestCase):
def setUp(self):
self.performer = Performer.objects.create(name='Craig Dennis')
self.song = Song.objects.create(
title='I Wanna Be Sedated',
artist='The Ramones',
length=149,
performer=self.performer)
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')
self.assertContains(resp, str(self.song))
Can't pass last line although i pass all tests in Eclipse
from django.db import models
# Create your models here.
class Performer(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Song(models.Model):
title = models.CharField(max_length=100)
artist = models.CharField(max_length=250)
performer = models.ForeignKey(Performer)
length = models.IntegerField(default=0)
class Meta:
ordering = ['artist', ]
def __str__(self):
return '{} by {}'.format(self.title, self.artist)
from django.shortcuts import render, get_object_or_404
from .models import Song, Performer
# Create your views here.
def songs_list(request):
songs = Song.objects.all()
return render(request, 'songs/song_list.html', {'songs': songs})
def songs_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)
song = performer.song_set.get(pk=pk)
context = {
'performer': performer,
'song': song
}
return render(request, 'songs/performer_detail.html', context)
{% extends 'base.html' %}
{% block title %}{{ performer }}{% endblock %}
{% block content %}
<h2>{{ performer }}</h2>
{% endblock %}
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsYou are very close. In the view performer_detail
you pass song
in the context but do not include a {{ song }}
in the performer_detail.html
template.
Dmitry Karamin
10,099 Pointsi thought that i can't change templates...
Chris Freeman
Treehouse Moderator 68,441 PointsSorry if that wasn't clear. If the challenge allows you to paste code as part of the solution then changing the file is usually fair game.