Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Django Basics Test Time Test our article detail template

Heidi Ulrich
Heidi Ulrich
4,624 Points

Why does my second test not pass?

I'm in the second step of this challenge, and worked out the test. I've looked up the Django documentation to see how assertContains works, and it seems I'm doing it right. I don't know if I address the name value correctly, or have set the .pk right. Please help :)

ps. if anyone has helpful documentation on pk's, please link!

import datetime

from django.core.urlresolvers import reverse
from django.test import TestCase

from .models import Article, Writer

class ArticleDetailViewTestCase(TestCase):
    '''Tests for the Article detail view'''

    def setUp(self):
        self.writer = Writer.objects.create(
            name='Kenneth Love',
            bio='Your friendly, local Python teacher'
        self.article = Article.objects.create(
          headline='Article 0',
          content='Something about 0',

    def test_detail_template(self):
        resp = self.client.get(reverse('articles:detail', kwargs={'pk': self.article.pk}))

    def test_detail_template_writer(self):
        resp = self.client.get(reverse('articles:detail', kwargs={'pk': self.article.pk}))
        self.assertContains(resp, resp.context['name'])
{% extends 'base.html' %}

{% block content %}
<h1>{{ article.title }}</h1>
<time>{{ article.publish_date }}</time>
<p>By {{ article.writer.name }}</p>
{{ article.content }}
{% endblock %}
Boris Ivan Barreto
Boris Ivan Barreto
6,837 Points

Hi Heidi,

You just need to change a bit the assertion in the test_detail_template_writer test.

def test_detail_template_writer(self):
    '''Make sure the article writer's name is in the rendered output'''
    resp = self.client.get(reverse('articles:detail', kwargs={'pk': self.article.pk}))
    self.assertContains(resp, self.article.writer.name)

2 Answers

Heidi Ulrich
Heidi Ulrich
4,624 Points

Boris' answer solved this for me.

Heidi Ulrich
Heidi Ulrich
4,624 Points

Hi Boris, thanks - that worked for me! Some times it can be so close, and a decent error would do wonders. :)

Unfortunately I cannot mark your answer as best, as you posted it as a comment. Repost if you like and I will mark it.