Reverse () increments NoReverseMatch when two applications use the same namespace

I am trying to use reverse()in unit test, but it cannot resolve the url.

tests.py:

from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase

class MyTest(APITestCase):
    def test_cust_lookup(self):
        url = reverse('v1:customer')
        # Other code

In the first line of the test, the test case fails:

NoReverseMatch: Reverse for 'customer' with arguments '()' and keyword arguments '{}'  not found. 0 pattern(s) tried: []

Did you try “0 patterns” means the test can't even find the root urls.py? Or is there something else that I configured incorrectly?

proj/settings.py:

ROOT_URLCONF = 'proj.urls'

proj/myapp/urls.py:

from django.conf.urls import url
import views

urlpatterns = [
    url(r'cust/$', views.CustomerView.as_view(), name='customer')
]

proj/urls.py:

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^api1/v1/', include('myapp1.urls', namespace='v1')),
    url(r'^api2/v1/', include('myapp2.urls', namespace='v1'))
]
+4
source share
2 answers

You do not have different applications in the same URL namespace, Django does not cope with this and does not tell you about it. Change the namespaces to be unique for each application.

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^api1/v1/', include('myapp1.urls', namespace='app1-v1')),
    url(r'^api2/v1/', include('myapp2.urls', namespace='app2-v1'))
]

Return URL c reverse('app1-v1:customer').

+2
source

Your setting should be

ROOT_URLCONF = 'urls'

not

ROOT_URLCONF = 'ommodels.urls'
0

All Articles