Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

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

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. I have tried changing the app_name, namespace, url name; 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:

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

"""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
question from:https://stackoverflow.com/questions/65910585/has-anyone-successfully-used-reverse-with-namespaces-rest-framework-and-host-pa

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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. This allows me to include the host path so the reverse will look like this reverse('users:post_user_feedback', host="api"), data=data).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...