我正在尝试在单元测试中使用reverse()
,但无法解析网址。
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
测试的第一行测试用例失败,出现以下错误:
NoReverseMatch: Reverse for 'customer' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
“尝试0模式”是否意味着测试甚至无法找到根urls.py
?或者还有其他我错误配置的内容?
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'))
]
答案 0 :(得分:3)
在相同的网址命名空间下不能使用不同的应用,Django无法处理它并且没有告诉您这是问题所在。将名称空间更改为每个应用程序的唯一名称。
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'))
]
使用reverse('app1-v1:customer')
撤消网址。
答案 1 :(得分:0)
您的设置应为
ROOT_URLCONF = 'urls'
不
ROOT_URLCONF = 'ommodels.urls'