视图集中的反向(事物)django rest 框架

reverse(thing) django rest framework in a viewset

主要问题是:什么是 rest 框架 ViewSet 友好的方式来制作反向路由?

在 django cookiecutter 站点中,我有 api_router.py 这个

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') = }")

哪个错误说反向找不到学生列表

学生 model.py 有一个符合标准的 StudentViewSet

class StudentViewSet(
    RetrieveModelMixin,
    CreateModelMixin,
    ListModelMixin,
    UpdateModelMixin,
    GenericViewSet,
):

所以它应该有创建学生列表所需的东西

该打印语句的输出显示学生列表

**** 已编辑....***

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

错误结尾如下:

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)

既然你设置了一个app_name,你将不得不在reverse中使用它,所以:

reverse('api:student-list')