The main question is: What is the rest framework ViewSet friendly way to make the reverse routes?
In a django cookiecutter site I have api_router.py with this
router.register(r"users", UserViewSet)
router.register(r"userprofile", UserProfileViewSet, basename="userprofile")
router.register(r"student", StudentViewSet)
urlpatterns = router.urls
app_name = "api"
print(f"we have routes")
for route in router.urls:
print(route)
print(f"{reverse('student-list') = }")
which errors out saying that reverse can't find student-list
the student model.py has a StudentViewSet with the standard
class StudentViewSet(
RetrieveModelMixin,
CreateModelMixin,
ListModelMixin,
UpdateModelMixin,
GenericViewSet,
):
so it should have the stuff needed to create the student-list
the output of that print statement is showing student-list
**** edited ....***
athenaeum_local_django | <URLPattern '^student/$' [name='student-list']>
athenaeum_local_django | <URLPattern '^student\.(?P<format>[a-z0-9]+)/?$' [name='student-list']>
athenaeum_local_django | <URLPattern '^student/(?P<userprofile__user__username>[^/.]+)/$' [name='student-detail']>
athenaeum_local_django | <URLPattern '^student/(?P<userprofile__user__username>[^/.]+)\.(?P<format>[a-z0-9]+)/?$' [name='student-detail']>
The end of the error is as follows:
ahenaeum_local_django | File "/app/config/urls.py", line 29, in <module>
athenaeum_local_django | path("api/", include("config.api_router")),
athenaeum_local_django | File "/usr/local/lib/python3.9/site-packages/django/urls/conf.py", line 34, in include
athenaeum_local_django | urlconf_module = import_module(urlconf_module)
athenaeum_local_django | File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
athenaeum_local_django | return _bootstrap._gcd_import(name[level:], package, level)
athenaeum_local_django | File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
athenaeum_local_django | File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
athenaeum_local_django | File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
athenaeum_local_django | File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
athenaeum_local_django | File "<frozen importlib._bootstrap_external>", line 850, in exec_module
athenaeum_local_django | File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
athenaeum_local_django | File "/app/config/api_router.py", line 22, in <module>
athenaeum_local_django | print(f"{reverse('student-list') = }")
athenaeum_local_django | File "/usr/local/lib/python3.9/site-packages/django/urls/base.py", line 86, in reverse
athenaeum_local_django | return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
athenaeum_local_django | File "/usr/local/lib/python3.9/site-packages/django/urls/resolvers.py", line 698, in _reverse_with_prefix
athenaeum_local_django | raise NoReverseMatch(msg)
athenaeum_local_django | django.urls.exceptions.NoReverseMatch: Reverse for 'student-list' not found. 'student-list' is not a valid view function or pattern name.
Unexpected API error for athenaeum_local_django (HTTP code 500)
Since you have set an app_name
, you will have to use it in reverse
, so:
reverse('api:student-list')