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 trialTJ Takei
13,362 Points'karaoke.song' is not a registered namespace
I have no idea why this test fails:
ERROR: test_song_list_view (songs.tests.ViewTests)
The song_list view should:
Traceback (most recent call last):
File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/django/core/urlresolvers.py", line 586, in reverse
extra, resolver = resolver.namespace_dict[ns]
KeyError: 'karaoke.song'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/django/template/defaulttags.py", line 507, in render
current_app=current_app)
File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/django/core/urlresolvers.py", line 596, in reverse
key)
django.core.urlresolvers.NoReverseMatch: 'karaoke.song' is not a registered namespace
...
I have been trying long time and gave up. Please help.
from django.db import models
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)
length = models.IntegerField(default=0)
def __str__(self):
return self.title + " by " + self.artist
from django.shortcuts import render, get_object_or_404
from .models import Song, Performer
def performer_detail(request, pk):
performer = get_object_or_404(Performer, pk=pk)
return render(request, 'songs/performer_detail.html', {'performer': performer})
def song_list(request):
songs = Song.objects.all()
return render(request, 'songs/song_list.html', {'songs': songs})
def song_detail(request, performer_pk, song_pk):
song = get_object_or_404(Song, performer_id=performer_pk, pk=song_pk)
return render(request, 'songs/song_detail.html', {'song': song})
{% extends 'base.html' %}
{% block title %}{{ performer }}{% endblock %}
{% block content %}
<h2>{{ performer }}</h2>
{% endblock %}
4 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsFound the issue. In karaoke/songs/templates/songs/song_list.html
there is a reference to the namespace song
:
{% 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' pk=song.pk %}">{{ song }}</a>
performed by
{# In THIS url, song:performer should be songs:performer #)
<a href="{% url 'song:performer' pk=song.performer.pk %}">{{ song.performer }}</a>
</li>
{% endfor %}
</ul>
{% endblock %}
Thank you for a challenging question! Happy coding!
TJ Takei
13,362 PointsThat's it, thanks!
Chris Freeman
Treehouse Moderator 68,441 PointsIn one of your templates I am guessing you have "karaoke.song" as a {% url %}
argument instead of "karaoke:song" (using a colon to indicate the namespace karaoke. See URL namespaces docs
TJ Takei
13,362 Pointshmm.. I fail to see "karaoke.song" as a {% url %} arg in my template files below.
..\karaoke\templates\base.html:
6 <body>
7 <h1>Django Karaoke!</h1>
8: <p><a href="{% url 'songs:list' %}">Upcoming Songs</a></p>
9 </body>
10 </html>
..\karaoke\templates\songs\song_detail.html:
7
8 <p>By {{ song.artist }}</p>
9: <p>Sung by <a href="{% url 'songs:performer' pk=song.performer.pk %}">{{ song.performer }}</a></p>
10 {% endblock %}
..\karaoke\templates\songs\song_list.html:
9 {% for song in songs %}
10 <li>
11: <a href="{% url 'songs:detail' pk=song.pk %}">{{ song }}</a> performed by <a href="{% url 'songs:performer' pk=song.performer.pk %}">{{ song.performer }}</a>
12 </li>
13 {% endfor %}
I'm still in dismay. Please help.
[MOD: added ``` markdown formatting -cf]
Chris Freeman
Treehouse Moderator 68,441 PointsTJ, Can you please post your karaoke/urls.py
and your songs/urls.py
? Thanks.
TJ Takei
13,362 PointsHi Chris, Thank you for taking a look at these.
karaoke/urls.py:
"""karaoke URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Add an import: from blog import urls as blog_urls
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views
urlpatterns = [
url(r'^songs/', include('songs.urls', namespace='songs')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.home, name='home'),
]
urlpatterns += staticfiles_urlpatterns()
songs/urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'performer/(?P<pk>\d+)/$', views.performer_detail, name='performer'),
url(r'song/(?P<pk>\d+)/$', views.song_detail, name='detail'),
url(r'^$', views.song_list, name='list'),
]
[MOD: adjusted formatting -cf]]
Chris Freeman
Treehouse Moderator 68,441 PointsI don't see any issue with the url files, they are the same as mine.
TJ Takei
13,362 PointsI am running both local and the workspace. Here's a link to the workspace: https://w.trhou.se/wmlfzbpgm2
TJ
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsShould have asked earlier: are you running on a local system or in the Workspaces?
If in the workspaces can you create a Snapshot (camera icon in upper right) of your workspace and post the link?