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

I can't spot the mistake in 'Writer view'

Now we need to create the view. I've already made the URL and template for you.

Create a view named writer_detail that takes a pk argument. It should .get() the Writer with the requested pk and render() the "articles/writer_detail.html" template. Provide the Writer in the context as "writer".


Bummer! Make sure the 'articles/writer_detail.html' template is used.

from django.shortcuts import render

from .models import Article
from .models import Writer

def article_list(request):
    articles = Articles.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})

I can't spot the mistake. Can someone help me out.

Filipe Fernandes
Filipe Fernandes
17,224 Points

This is obviously an extremely late comment, but on line 6 where it says: articles = Articles.objects.all(), Articles should be Article, I believe

3 Answers

Not sure. Your code looks the same as mine, and it passed:

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})
Dillon Reyna
Dillon Reyna
9,531 Points

I'm confused on this one.

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

What exactly is 'pk'? I didn't quite understand from the video. Why are we .get-ing the 'pk', and why do we set pk equal to itself? And can someone explain the purpose of context-dicts?