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 trialLeo Marco Corpuz
18,975 Pointsarticle_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>/'
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})
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
231,269 PointsIt 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
18,975 PointsThanks! Would you recommend I take these courses before continuing with the Django course?
Steven Parker
231,269 PointsI 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.
matth89
17,826 PointsFor 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
231,269 PointsGood update about Django, Matt.
But Django aside, I agree that for programming in general, it's good to have regex in your toolbox!
Leo Marco Corpuz
18,975 PointsLeo Marco Corpuz
18,975 PointsI 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
231,269 PointsSteven Parker
231,269 PointsThose 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.