Home > Enterprise >  django.urls.exceptions.NoReverseMatch: 'users' is not a registered namespace inside '
django.urls.exceptions.NoReverseMatch: 'users' is not a registered namespace inside '

Time:08-27

#main url

from django.contrib import admin

from django.urls import path,include

urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/', include('users.api.urls',namespace='v1')),

]

#app url

from django.urls import path
from .views import ExampleView

app_name='users'

urlpatterns=[
path("", ExampleView.as_view(), name='home')
]

app view

from rest_framework.views import Response, APIView


class ExampleView(APIView):

def get(self, request):
    return Response({"message": "hello world"})

test app

from django.urls import reverse
from rest_framework.test import APISimpleTestCase


class TestUser(APISimpleTestCase):

def setUp(self):

    self.path1 = reverse('v1:users:home')

def test_user_path(self):
    print(self.path1)

what is wrong with this code? why it return django.urls.exceptions.NoReverseMatch: 'users' is not a registered namespace inside 'v1'

CodePudding user response:

Try the following:

reverse('v1:home')

You have already changed the namespace of users to v1. Since users is not a URL path name in the app's urls.py, you get an error.

  • Related