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 trialVarsha Joshi
Courses Plus Student 7,069 PointsHello, I am getting a KeyError when trying to run my tests My code snapshot: https://w.trhou.se/ukyrawz5yq
Traceback (most recent call last): File "/home/treehouse/workspace/karaoke/songs/tests.py", line 73, in test_performer_detail_view self.assertIn(resp.context['performer'], self.performer.name) File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/django/test/utils.py", line 6 4, in getitem raise KeyError(key) KeyError: 'performer'
from django.db import models
# * Performer model should: # * have a name # * return the name when turned into a string
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()
performer = models.ForeignKey(Performer)
def __str__(self):
return '{} by {}'.format(self.title, self.artist)
from django.http import HttpResponse
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': 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})
{% extends 'base.html' %}
{% block title %}{{ performer.name }}{% endblock %}
{% block content %}
<h2>{{ performer.name }}</h2>
{% for song in songs %}
{{ song }}
{% endfor %}
{% endblock %}
4 Answers
Renato Guzman
51,436 Pointsreturn render(request, 'songs/performer_detail.html', {'Performer': performer})
Performer is uppercase in your context, it should be lowercase. The test (because of the error) is self.assertIn(resp.context['performer'], self.performer.name)
So in the context the performer with lowercase does not exist.
Varsha Joshi
Courses Plus Student 7,069 PointsI finally solved the challenge.:-) Thanks for helping me out.
Varsha Joshi
Courses Plus Student 7,069 PointsThanks Renato, your suggestion solved the key error. But now I get Performer is not iterable. I dont get why the song tests work and performer ones dont..
Renato Guzman
51,436 PointsIt seems that your are missing context_data. Notice that you are iterating songs
and the variable songs
is not in the context from performer_detail. You have to add 'songs' to the context.