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

Hjalte Bøgehave
Hjalte Bøgehave
6,367 Points

course detail test: NoReverseMatch

Im trying to write my own test for course_detail, but every time i try to run it i get the error:

'django.core.urlresolvers.NoReverseMatch: Reverse for 'detail' with arguments '()' and keyword arguments '{}' not found.'
Tests.py
    class CourseViewsTests(TestCase):
        def setUp(self):
            self.course = Course.objects.create(
                title="Python Testing",
                description="Learn to write tests in Python"
            )
            self.course2 = Course.objects.create(
                title="New Course",
                description="A new course"
            )
            self.step = Step.objects.create(
                title="Introduction to Doctests",
                description="Learn to write tests in your docstrings.",
                course = self.course
            )

        def test_course_list_view(self):
            resp = self.client.get(reverse('courses:list'))
            self.assertEqual(resp.status_code, 200)
            self.assertIn(self.course, resp.context['courses'])
            self.assertIn(self.course2, resp.context['courses'])

        def test_course_detail_view(self):
            resp = self.client.get(reverse('courses:detail'))
            self.assertEqual(resp.status_code, 200)
            self.assertIn(self.course, resp.context['course'])
            self.assertIn(self.course2, resp.context['course'])
views.py
from django.shortcuts import get_object_or_404, render

from .models import Course, Step


def course_list(request):
    courses = Course.objects.all()
    return render(request, 'courses/course_list.html', {'courses': courses})


def course_detail(request, pk):
    course = get_object_or_404(Course, pk=pk)
    return render(request, 'courses/course_detail.html', {'course': course})

def step_detail(request, course_pk, step_pk):
    step = get_object_or_404(Step, course_id=course_pk, pk=step_pk)
    return render(request, 'courses/step_detail.html', {'step': step})
urls.py
from django.conf.urls import url

from . import views

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'),
]

1 Answer

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

The detail url expects a pk but you didn't specify any pk in your test.

You can add a pk to your url like this:

reverse('your_url:here', kwargs={'pk': pk_value_here})

For more information: docs about reverse