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 View Tests

Reverse Throwing an Error for Step Detail View Test

I'm trying to work through writing course detail and step detail view tests like Kenneth recommended at the end of the video. I'm running into the following error for my step detail view test:

TypeError: step_detail() got an unexpected keyword argument 'course_pk'

If anyone has any suggestions as to why I'm getting this error it would be greatly appreciated! Here is my code:

tests.py

class CourseViewsTest(TestCase):
    def setUp(self):
        self.course = Course.objects.create(
            title="Python testing",
            description="learn to write tests"
        )
        self.step = Step.objects.create(
            title="I'm a step",
            description="I'm a step description",
            course=self.course
        )

    def test_step_detail_view(self):
        resp = self.client.get(reverse('courses:step', kwargs={'course_pk':self.course.pk, 'step_pk':self.step.pk}))
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(self.step, resp.context['step'])

urls.py

urlpatterns = [
    url(r'^$', views.course_list, name='list'),
    url(r'(?P<course_pk>\d+)/(?P<step_pk>\d+)$', views.step_detail, name='step'),
    url(r'(?P<pk>\d+)/$', views.course_detail, name='detail'),
]

views.py

def step_detail(request, pk):
    step = get_object_or_404(Step, course_id=course_pk, pk=pk)
    return render(request, 'courses/step_detail.html', {'step': step})

1 Answer

Haydar Al-Rikabi
Haydar Al-Rikabi
5,971 Points

In your views.py:

def step_detail(request, course_pk, pk): # course_pk was needed here