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

Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

article_detail view challenge

I'm stuck on the second part. I keep getting an error saying I should make sure the URL should be '/article/<pk>/'

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),
    url(r'/article/<pk>/'),
]

2 Answers

Steven Parker
Steven Parker
231,236 Points

It looks like you still need to add the second argument to "url", and expand the regex to include the specification for "one or more digits". Take a look at the existing line for "writer", it fulfills similar requirements and can serve as a model.

You may also need to change the order of the lines.

Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

I don’t understand the β€œone or more digits” part as well as the line order. I’m confused with β€˜?P’, β€˜d+’ and β€˜$’ in the writer model.

Steven Parker
Steven Parker
231,236 Points

Those characters are what require the "one or more digits" and are the syntax of Regex (Regular Expressions). These courses might be of interest if you're not familiar with them: Introduction to Regular Expressions and Regular Expressions in Python.

But for this challenge you can probably get by just substituting the appropriate terms.

Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

Thanks! Would you recommend I take these courses before continuing with the Django course?

Steven Parker
Steven Parker
231,236 Points

I don't know the course well enough to say. If you can get by this time, try moving ahead and see if continues to be an issue.

For the record, regular expressions are no longer used in Django 2.0 and up. So don't sweat it too hard if you plan to use more recent versions of Django. That being said, if you end up working in Django a lot, you'll likely run into regular expressions here and there. Not to mention, it is a pretty good tool to have at least some understanding of.

Steven Parker
Steven Parker
231,236 Points

Good update about Django, Matt.

But Django aside, I agree that for programming in general, it's good to have regex in your toolbox! :wink: