【问题标题】:reverse(thing) django rest framework in a viewset视图集中的反向(事物)django休息框架
【发布时间】:2022-01-10 04:08:19
【问题描述】:

主要问题是:什么是其余框架 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('api:student-list')
  • 说真的:设置 ^ 作为答案。我现在正在处理其他错误。 :)

标签: django django-rest-framework routes cookiecutter-django


【解决方案1】:

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

reverse('api:student-list')

【讨论】:

    猜你喜欢
    • 2019-04-08
    • 1970-01-01
    • 2013-04-08
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多