在我的urls.py
文件中,我有:
from myapp import views
...
(r'^categories/$', views.categories)
categories
里面的视图函数在哪里myapp/views.py
。没有其他 URLconf 行参考views.categories
。
在一个单元测试文件中,我试图使用 来获取这个 URL django.core.urlresolvers.reverse()
,而不是仅仅复制 '/categories/' (DRY 和所有这些)。所以我有:
from django.core.urlresolvers import reverse
from myapp import views
...
url = reverse(views.categories)
当我运行我的测试时,我得到一个NoReverseMatch
错误:
NoReverseMatch: Reverse for '<function categories at 0x1082f30>' with arguments '()' and keyword arguments '{}' not found.
如果我将 URL 模式设为命名模式,它匹配得很好,如下所示:
url(r'^categories/$', views.categories, 'myapp-categories')
并使用模式名称来匹配它:
url = reverse('myapp-categories')
但据我从文档中可以看出,reverse
我不需要将其设为命名 URL 模式即可使用reverse
。
任何想法我做错了什么?