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 Article detail view

Django basics task challenge

Hi for the Django basic course I keep getting the error (Bummer! Make sure the 'articles/article_detail.html' template is used.) for the task below:

Our view needs an URL. Add a new url to article/urls.py. The pattern should be "article/" and then the pk argument, which should be one or more digits.

The code I have is as follows:

Thanks.

articles/views.py
from django.shortcuts import render, get_object_or_404

from .models import Article, Writer


def article_list(request):
    articles = Article.objects.all()
    return render(request, 'articles/article_list.html', {'articles': articles})


def writer_detail(request, pk):
    writer = Writer.objects.get(pk=pk)
    return render(request, 'articles/writer_detail.html', {'writer': writer})


def article_detail(request, pk):
    article = get_object_or_404(Article, pk=pk)
    return render(request, 'articles/article_detail.html', {'article': article})
articles/urls.py
from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'writer/(?P<pk>\d+)/$', views.writer_detail),
    url(r'', views.article_list),
    url(r'articles/(?P<pk>\d+)/$', views.article_detail),
]

[MOD: removed redundant code]

4 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Hi Steve, You have a typo in urls.py:

urlpatterns = [
    # Task 2 of 3
    # Our view needs an URL. Add a new url to article/urls.py. The pattern
    # should be "article/" and then the pk.
    url(r'article/(?P<pk>\d+)/$', views.article_detail),  # <-- article not 'articles'

    url(r'writer/(?P<pk>\d+)/$', views.writer_detail),
    url(r'', views.article_list),
]

Thanks but it still does not pass

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Hi Steve, I think you might have found a bug in the treehouse grader. Compare your URL order to mine. Put the article_detail URL first, instead of last.

This passes:

urlpatterns = [
    url(r'article/(?P<pk>\d+)/$', views.article_detail),
    url(r'writer/(?P<pk>\d+)/$', views.writer_detail),
    url(r'', views.article_list),
]

This does NOT pass:

urlpatterns = [
    url(r'writer/(?P<pk>\d+)/$', views.writer_detail),
    url(r'', views.article_list),
    url(r'article/(?P<pk>\d+)/$', views.article_detail),
]

Tagging Kenneth Love to check the challenge validation.

Jesse Pavlis
Jesse Pavlis
18,088 Points

Hey Chris,

That second snippet not passing is correct. Django's URL dispatcher goes through urls in order and stops at the first one that matches.

https://docs.djangoproject.com/en/1.9/topics/http/urls/#how-django-processes-a-request

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Hey Jesse,

I agree that the second snippet should pass, but it doesn't currently. The URLs are non-overlapping so ordering, in this case, shouldn't matter.

The suggestion to reorder is merely a workaround to pass the challenge. I think there is a bug in the challenge grader.

Thanks and I also did not think the order mattered as there was no overlap. Thanks for the workaround.