Does not get the url from reverse with DRF DefaultRouter

I am trying to get the urls for writing the tests using the reverse function. But I'm getting the error

Errors :- Reverse for 'notes' not found. 'notes' is not a valid view function or pattern name.

urls.py file

from django.contrib import admin
from django.urls import path, include
from core import views
from rest_framework.routers import DefaultRouter

router = DefaultRouter()

router.register("notes",views.NoteModelViewSet)

urlpatterns = [
    path("",include(router.urls)),
]

and test.py file

def test_get_all_notes_path(self):
        url = reverse('notes')
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

I'm getting error for line url = reverse('notes') that I have mentioned above. My Model and routes everything working fine. Only I'm not get the url in reverse funtion.

whether you set basename or not you can do reverse with url-path-or-basename-list

url = reverse('notes-list')

set the basename for your router as an option

router.register("notes",views.NoteModelViewSet, basename='notes')

because basename will create routes with the default name

The example above would generate the following URL patterns:

URL pattern: ^notes/$ Name: 'notes-list'
URL pattern: ^notes/{pk}/$ Name: 'notes-detail'
Back to Top