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 Final Details url Tag

NoReverseMatch: Error During Template Rendering

I am having trouble figuring out why I am getting this error: NoReverseMatch at / Reverse for 'deets' with arguments '(u'test-post',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['post/(?P<slug>\d+)$']

Template Highlighted Error:

<a href="{{ post.get_absolute_url }}">{{ post.title }}</a>

models function:

    def get_absolute_url(self):
        return reverse('deets', args=[self.slug])

blog.urls:

urlpatterns = [
    url(r'(?P<slug>\d+)$', views.post, name='deets'),
]

urls:

urlpatterns = [
    url(r'^$', views.index),
    url(r'^post/', include('blog.urls')),
]

blog.views:

def post(request, slug):
    post = get_object_or_404(Post, slug=slug)
    return render(request, 'blog/post.html', {'post': post})

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

TL;DR: reverse for deets is being passed the string u'test-post' from self.slug. The reverse is looking for digits to match \d+

Perhaps change blog.urls to match word chars and hyphen:

urlpatterns = [
    url(r'(?P<slug>[\w-]+)$', views.post, name='deets'),
]

Oh, wow... so it was a regex error all along. Thanks!