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 Django Basics Test Time Test our article list view

Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

Django view testing

Can someone please check my code on this challenge? Thanks.

articles/tests.py
import datetime

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

from .models import Article, Writer

class ArticleListViewTestCase(TestCase):
    '''Tests for the Article list 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()
        )
        for x in range(1, 3):
            Article.objects.create(
                writer=self.writer,
                headline='Article {}'.format(x),
                content='Something about {}'.format(x),
                publish_date=datetime.datetime.today()
            )
       def test_article_list_view(self)
            resp=self.client.get("articles:list")
            self.assertIn(self.article,resp.context[articles])
articles/views.py
from django.shortcuts import get_object_or_404, render

from .models import Article, Writer


def article_list(request):
    articles = Article.objects.all()
    return render(request, 'articles/article_list.html', {'articles': articles})


def article_detail(request, pk):
    article = get_object_or_404(Article, pk=pk)
    return render(request, 'articles/article_detail.html', {'article': article})


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

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Leo,

You have a few problems with your code.

First, you have an indentation error in your method definition. It should line up with the definition of setUp.

Next, you have a syntax error because are not finishing the method definition with a colon character.

Once you resolve these two issues, your error message should change from just "Try Again" to "Your test didn't pass".

Inside your method you are passing a string representing a named route ("articles:list") to self.client.get(). However, this doesn't take a route name, it takes a url (e.g., '/articles/list'). To get from a named route to a url you can use the reverse function.

Also, when you access the context dictionary you are passing it articles which is an undeclared variable name instead of "articles" which is a string that Python can use to index into the dictionary.

Hope that clears everything up for you.

Cheers,

Alex