Django Validation - NoReverseMatch

Why does this test not work?

This is my view.py:

class ObjectDetailView(LoginRequiredMixin, DetailView):
    template_name = "object-detail.html"
    model = Object
    slug_field = 'username'

    def dispatch(self, request, *args, **kwargs):
        ....

urls.py:

url(r'^object/details/(?P<slug>[-\w.]+)/$', ObjectDetailView.as_view(), name='object-details'),

tests.py:

class ObjectViewsTestCase(TestCase):
    fixtures = ['/app/fixtures/object_fixture.json', ]

    def test_object_details(self):
        user = User.objects.get(id=1)
        self.client.login(username=user.username, password=user.password)
        resp = self.client.get(reverse('object-details', kwargs={'slug': user.username}))
        self.assertEqual(resp.status_code, 200)

My mistake:

NoReverseMatch: the converse for 'part objects' with arguments' ()' and the keyword arguments' {'slug': u'admin '}' were not found. 0 templates (s): []

+4
source share
1 answer

Do you have ROOT_URLCONFin your settings? If so, make sure that these parameters are loaded when starting the tests. Or you can add the following to your tests:

class ObjectViewsTestCase(TestCase):
    fixtures = ['/app/fixtures/object_fixture.json', ]
    urls = 'path.to.your.urls'  # for instance 'base.app.urls'
+4
source

Source: https://habr.com/ru/post/1542171/


All Articles