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 trialAndreas cormack
Python Web Development Techdegree Graduate 33,011 Pointsdjango testing the article detail view.
How do you pass the pk to the reverse function?
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', self.article}))
self.assertEqual(resp.status_code,200)
self.assertTemplateUsed(response,'articles/article_detail.html')
def test_detail_template_writer(self):
'''Make sure the article writer's name is in the rendered output'''
{% extends 'base.html' %}
{% block content %}
<h1>{{ article.title }}</h1>
<time>{{ article.publish_date }}</time>
<p>By {{ article.writer.name }}</p>
{{ article.content }}
{% endblock %}
5 Answers
MUZ140734 Tinashe Chibwe
16,960 Points#so...
resp = self.client.get(reverse('articles:detail', kwargs={'pk': self.article.pk}))
#and use resp not response in your self.assertTemplateUsed so ..
self.assertTemplateUsed(resp,'articles/article_detail.html')
[edit format -cf]
Chris Freeman
Treehouse Moderator 68,441 Pointsformat updated, up-voted, and marked Best Answer
Ronny Kibet
47,409 PointsInclude .name
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) #include .name
MUZ140734 Tinashe Chibwe
16,960 PointsTry passing it in as kwargs
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsThanks that worked. Although my second part of the challenge now errors out with 'assertContains not used' this is what I am done
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)
Michael Covington
15,290 PointsI suspect this is a bug in the objective. It works if you move the response to setUp()
:
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()
)
self.response = self.client.get(reverse('articles:detail', kwargs={'pk': self.article.pk}))
def test_detail_template(self):
'''Make sure the `articles/article_detail.html` template is used'''
self.assertTemplateUsed(self.response, 'articles/article_detail.html')
def test_detail_template_writer(self):
'''Make sure the article writer's name is in the rendered output'''
self.assertContains(self.response, self.article.writer.name)
Kyle Salisbury
Full Stack JavaScript Techdegree Student 16,363 PointsMichael has it right, however he is missing the _writer part in the def test_detail_template.
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsAside on cleaning up formatting: