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

Simon Amz
Simon Amz
4,606 Points

Karaoke project

Hi there,

for the "Karaoke" exercise, I tried on my text editor, and apparently I succeed, however, I used django 2.0 and some functionalities as 'path' instead of 'url'. So I still got an error but on my computer it works.

Here is my model:

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.CharField(max_length=255)
    performer = models.ForeignKey(Performer, on_delete=models.CASCADE)
    length = models.IntegerField(default=0)

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

Here is my 'url' file from karaoke:

from django.conf.urls import include, url
from django.contrib import admin
from django.urls import path
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('songs/', include('songs.urls', namespace='songs')),
    url('', views.home, name='home'),
]

Here is my 'url' file from Songs directory:

from django.urls import path

from . import views

app_name='songs'
urlpatterns = [
    path('', views.song_list, name='list'),
    path('song<int:song_pk>/', views.song_detail, name='detail'),
    path('performer<int:performer_pk>/', views.performer_detail, name='performer'),
]

Here is my 'views' file:

from django.shortcuts import get_object_or_404, render
from .models import Song, Performer


def home(request):
    return render(request, 'home.html')

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

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

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

Here is the 'song_list.html' template:

{% extends 'base.html' %}

{% block title %}Upcoming Songs{% endblock %}

{% block content %}
<h2>Upcoming Songs</h2>

<ul>
    {% for song in songs %}
    <li><a href="{% url 'songs:detail' song_pk=song.pk %}">{{ song }}</a> performed by <a href="{% url 'songs:performer' song.performer.pk %}">{{ song.performer }}</a></li>
    {% endfor %}
</ul>
{% endblock %}

Here is the 'song_detail.html' template:

{% extends 'base.html' %}

{% block title %}{{ song.title }}{% endblock %}

{% block content %}
<h2>{{ song.title }}</h2>

<p>By {{ song.artist }}</p>
<p>Sung by <a href="{% url 'songs:performer' performer_pk=song.performer.pk %}">{{ song.performer }}</a></p>
{% endblock %}

and here is the 'performer_detail' template:

{% extends 'base.html' %}
{% block title %}{{ performer }}{% endblock %}

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

1 Answer

That is because, when you submitting it - the Django version is 1.8 in the challenge