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

Challenge Task 1 of 2 Django Basic

I can't figure out why this code fails. Please help.

articles/tests.py
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',
            email='kenneth@teamtreehouse.com',
            bio='Your friendly, local Python teacher'
        )
        self.article = Article.objects.create(
          writer=self.writer,
          headline='Article 0',
          content='Something about 0',
          publish_date=datetime.datetime.today()
        )

    def test_detail_template(self):
        '''Make sure the `articles/article_detail.html` template is used'''
        resp = self.client.get(reverse('articles:detail',kwargs={'pk':self.article.pk}))
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(self.article, resp.context['article'])
        self.assertTemplateUsed(resp, 'articles/article_detail.html')

    def test_detail_template_writer(self):
        '''Make sure the article writer's name is in the rendered output'''
        resp = self.client.get(reverse('articles:writer',kwargs={'article_pk':self.article.pk, 'writer_pk':self.writer.pk}))
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(self.writer, resp.context['writer'])
articles/templates/articles/article_detail.html
{% extends 'base.html' %}

{% block content %}
<h1>{{ article.title }}</h1>
<time>{{ article.publish_date }}</time>
<p>By {{ article.writer.name }}</p>
{{ article.content }}
{% endblock %}

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,106 Points

In the second challenge you are still working on the articles:detail view.

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',
            email='kenneth@teamtreehouse.com',
            bio='Your friendly, local Python teacher'
        )
        self.article = Article.objects.create(
          writer=self.writer,
          headline='Article 0',
          content='Something about 0',
          publish_date=datetime.datetime.today()
        )

    def test_detail_template(self):
        '''Make sure the `articles/article_detail.html` template is used'''
        resp = self.client.get(reverse('articles:detail',kwargs={'pk':self.article.pk}))
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(self.article, resp.context['article'])
        self.assertTemplateUsed(resp, 'articles/article_detail.html')


    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}))  # <-- changed to article:detail
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(self.article, resp.context['article'])  # <-- changed to self.article
        self.assertContains(resp, self.article.writer.name)  # <-- add 'assertConains' check

Hi Chris, I overlooked article with writer. Thank you very much, TJ

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',
        email='kenneth@teamtreehouse.com',
        bio='Your friendly, local Python teacher'
    )
    self.article = Article.objects.create(
      writer=self.writer,
      headline='Article 0',
      content='Something about 0',
      publish_date=datetime.datetime.today()
    )

def test_detail_template(self):
    '''Make sure the `articles/article_detail.html` template is used'''
    resp = self.client.get(reverse('article:detail',kwargs={'pk':self.article.pk}))
    self.assertEqual(resp.status_code, 200)
    self.assertEqual(self.article, resp.context['article'])
    self.assertTemplateUsed(resp, 'articles/article_detail.html')

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