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

Dat Le Tran
Dat Le Tran
633 Points

I can't pass challenge Task 1 of 3 to create article_detail.

Here is my code, what's wrong with it?

articles/views.py
from django.shortcuts import render

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 = Article.objects.get(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),
]
bryonlarrance
bryonlarrance
16,414 Points

If you are working on Task 1 the only error I see in your code is the ':' in your article_detail method.

2 Answers

Hi Dat

Your article_detail view needs to use the get_object_or_404 function to manage bad pk's, it also seems you have not added a url for the article_detail view.

See below

def article_detail(request,pk):
    article = get_object_or_404(Article,pk=pk) # you missed this
    return render(request,'articles/article_detail.html',{'article':article})


# your article_detail url should look like this
url(r'article/(?P<pk>\d+)/$',views.article_details),

Happy coding !

Dat Le Tran
Dat Le Tran
633 Points

Hi Andreas cormack

Thanks for your reply. No, my code was ok, the task didn't ask for get_object_or_404 function.

It's the challenge checker bug, again, I logged out, logged in, refreshed, submitted again and it worked, it's terrible experience with teamtreehouse.

I faced this issue few times already.

Hi Dat

Part 3 of the task does as you to handle bad pk's, i.e its expecting to use the get_object_or_404 function. In terms of the challenge checker, i did notice if you try and copy paste code it doesn't seem to work properly. But on the other hand if you type the same code back again it works. Were you copy pasting code by any chance ? The only time i have ever faced problems with the code checker was when i try and solve a challenge with another method that the checker is not expecting.

Dat Le Tran
Dat Le Tran
633 Points

Hi Andreas cormack,

Were you copy pasting code by any chance ?

Yes, I did. The challenge editor breaks in Chrome since the latest update. So I write code in sublime and paste it back to the challenge editor.

Most of the time it is fine. But sometimes I need to refresh the page and submit again.