Home > Back-end >  Django Test - django.urls.exceptions.NoReverseMatch
Django Test - django.urls.exceptions.NoReverseMatch

Time:08-11

I got errors when running tests

django.urls.exceptions.NoReverseMatch: Reverse for 'interfacegraph' with no arguments not found. 1 pattern(s) tried: ['interfacegraph/(?P<device>[A-Z0-9.]{12,15})/(?P<interface>\\d )/(?P<graph>\\d )/graph/']

Here is the urls.py

 urlpatterns = [
        re_path(r'interfacegraph/(?P<device>[A-Z0-9.]{12,15})/(?P<interface>\d )/(?P<graph>\d )/graph',
        InterfaceGraphViewSet.as_view({'get': 'graph'}), name='interfacegraph'),
 ]

And heres the line of code in tests where the test throws errors

    response = self.client.get(
        reverse('interfacegraph'), {'device': 'device', 'interface': 'interface', 'graph': 'traffic'},
        content_type='application/json')

The viewset is extending generics viewset

  class InterfaceGraphViewSet(viewsets.GenericViewSet):
   

Any idea how to test this?

CodePudding user response:

You don't actually pass arguments to reverse function. Use it that way:

reverse('interfacegraph', kwargs={'device': 'device', 'interface': 'interface', 'graph': 'traffic'})

Also another problem is here:

(?P<device>[A-Z0-9.]{12,15})

You are passing 'device', that does not fit that regex. It checks uppercase letters and digits, so in given string you have exactly 0 occurencies. Learn about regex. You can also not use it at all, like:

path('interfacegraph/<str:device>/<int:interface>/<int:graph>/graph', ...)
  • Related