简体   繁体   English

有没有人成功地将 Reverse 与命名空间、rest_framework 和主机模式一起使用?

[英]Has anyone successfully used Reverse with namespaces, rest_framework and host patterns?

I have literally been trying to solve/ignore this issue for months now but I cannot test properly nor move on until I do.几个月来,我一直在尝试解决/忽略这个问题,但我无法正确测试,也无法继续前进。 I've tried using namespaces and hard-coding but I still get the same error.我试过使用命名空间和硬编码,但我仍然得到同样的错误。 'django.urls.exceptions.NoReverseMatch: 'users' is not a registered namespace' 'django.urls.exceptions.NoReverseMatch: 'users' 不是注册的命名空间'

If you can see a solution that I cannot, please assist me.如果您能看到我看不到的解决方案,请帮助我。 I have attached the url files and test that is giving me the error.我附上了 url 文件并进行了测试,这给了我错误。 I have tried changing the app_name, namespace, url name;我尝试更改 app_name、命名空间、url 名称; adding and removing said variables...same issue no matter what.添加和删除所述变量...无论如何都是相同的问题。 I'm stumped.我难住了。 Thank you in advance!先感谢您!

Test:测试:

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)

App named Users urls:名为用户网址的应用程序:

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 urls: 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')),
]

Host file:主机文件:

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"),
)

Main url file (only used for admin):主要 url 文件(仅用于管理员):

"""busTracker URL Configuration"""

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

app_name = "console"

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

Full Error trace:完整的错误跟踪:

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

So i actually figured it out on my own after digging deep into all of the documentation.因此,在深入研究了所有文档之后,我实际上是自己弄清楚了。 I was using from django.urls import reverse when I should have been using the django-host provided from django_hosts.resolvers import reverse .当我应该使用from django.urls import reverse提供的 django-host 时,我正在使用from django_hosts.resolvers import reverse This allows me to include the host path so the reverse will look like this reverse('users:post_user_feedback', host="api"), data=data) .这允许我包含主机路径,因此反向看起来像这样reverse('users:post_user_feedback', host="api"), data=data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM