0

几个月来,我一直在尝试解决/忽略这个问题,但我无法正确测试,也无法继续前进。我试过使用命名空间和硬编码,但我仍然得到同样的错误。'django.urls.exceptions.NoReverseMatch: 'users' 不是注册的命名空间'

如果您能看到我看不到的解决方案,请帮助我。我已附上 url 文件并进行了测试,这给了我错误。我尝试过更改 app_name、命名空间、url 名称;添加和删​​除所述变量...无论如何都是相同的问题。我难住了。先感谢您!

测试:

from rest_framework.test import APITestCase
from django.urls import reverse
from users.models import Feedback


class TestFeedbackAPI(APITestCase):
    def test_post_request_can_create_new_entity(self):
        data = {
            'subject': 'Test',
            'message': 'Testing on these hoes',
        }
        self.client.post(reverse('users:post_user_feedback'), data=data)
        self.assertEqual(Feedback.objects.count(), 1)

名为用户网址的应用程序:

from django.urls import path
from .views import ProfileDetail, SettingsDetail, FeedbackDetail

app_name = 'users'

urlpatterns = [
    # path("create/", views.UserCreate.as_view()),  # Sign up view, POST
    path('profile/', ProfileDetail.as_view(),
         name='get_user_profile'),
    path('settings/', SettingsDetail.as_view(),
         name='get_user_settings'),
    path('support/', FeedbackDetail.as_view(),
         name='post_user_feedback'),
]

API 网址:

"""busTracker API URL Configuration"""

from django.urls import include, path

urlpatterns = [
    path("auth/", include("djoser.urls")),
    path("auth/", include("djoser.urls.authtoken")),
    path('', include('core.urls', namespace='core')),
    path('', include('users.urls', namespace='users')),
]

主机文件:

from django.conf import settings
from django_hosts import patterns, host

host_patterns = patterns(
    "",
    host(r"www", "busTracker.template_urls", name="www"),
    host(r"console", settings.ROOT_URLCONF, name="console"),
    host(r"api", "busTracker.api_urls", name="api"),
)

主 url 文件(仅用于管理员):

"""busTracker URL Configuration"""

from django.contrib import admin
from django.urls import include, path

app_name = "console"

urlpatterns = [
    path("", admin.site.urls),
]

完整的错误跟踪:

ERROR: test_post_request_can_create_new_entity (tests.test_request.TestFeedbackAPI)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/kappalucky/Documents/Projects/TransportLLC-backend/venv/lib/python3.8/site-packages/django/urls/base.py", line 72, in reverse
    extra, resolver = resolver.namespace_dict[ns]
KeyError: 'users'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/kappalucky/Documents/Projects/TransportLLC-backend/tests/test_request.py", line 12, in test_post_request_can_create_new_entity
    self.client.post(reverse('users:post_user_feedback'), data=data)
  File "/Users/kappalucky/Documents/Projects/TransportLLC-backend/venv/lib/python3.8/site-packages/django/urls/base.py", line 83, in reverse
    raise NoReverseMatch("%s is not a registered namespace" % key)
django.urls.exceptions.NoReverseMatch: 'users' is not a registered namespace
4

1 回答 1

0

因此,在深入研究了所有文档之后,我实际上是自己弄清楚了。from django.urls import reverse当我应该使用提供的 django-host 时,我正在使用from django_hosts.resolvers import reverse。这允许我包含主机路径,因此反向看起来像这样reverse('users:post_user_feedback', host="api"), data=data)

于 2021-01-27T16:49:05.057 回答