Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Django Basics Test Time Django TDD

yan qingsong
yan qingsong
15,282 Points

Why not pass?

models.py

from django.db import models

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

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

views.py

from django.shortcuts import render, get_object_or_404

from .models import Song, Performer

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

def song_detail(request, pk): song = get_objects_or_404(Song, pk=pk) return render(request, 'songs/song_detail.html', {'song': song})

def performer_detail(request, pk): performer = get_objects_or_404(Performer, pk=pk) songs = Song.objects.all().filter(performer=performer) return render(request, 'songs/performer_detail.html', {'performer': performer, 'songs': songs})

performer_detail.html

{% extends 'base.html' %}

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

{% block content %} <h2> {{ performer }} </h2> {% for song in songs%} {{ song }} {% endfor %} {% endblock %}

songs/models.py
from django.db import models

# Write your models here

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.TextField()
    performer = models.ForeignKey('Performer')
    duration = models.DurationField(default=0)

    def __str__(self):
        return '{} by {}'.format(self.title, self.artist)
songs/views.py
from django.shortcuts import render, get_object_or_404

from .models import Song, Performer

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

def song_detail(request, pk):
    song = get_objects_or_404(Song, pk=pk)
    return render(request, 'songs/song_detail.html', {'song': song})

def performer_detail(request, pk):
    performer = get_objects_or_404(Performer, pk=pk)
    all_song = Song.objects.all()
    songs = filter(performer=performer, all_song)
    return render(request, 'songs/performer_detail.html', {'performer': performer, 'songs': songs})
songs/templates/songs/performer_detail.html
{% extends 'base.html' %}

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

{% block content %}
<h2>
  {{ performer }}
</h2>
    {% for song in songs%}
    {{ song }}
    {% endfor %}
{% endblock %}