我在使用reverse()
函数和视图函数作为参数时遇到问题。当我在默认的urls.py
文件中指定URL路由时,它工作正常。但是当我有import
到第二个urls.py
文件时,我得到了NoReverseMatch
-错误。反过来查看功能不能与多个urls.py文件一起使用
所以...这就是我的urls.py文件的外观。
demostore/demostore/urls.py
#demostore/demostore/urls.py
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('lindshop.urls', namespace="shop")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
demostore/lindshop/urls.py:
#demostore/lindshop/urls.py
urlpatterns = [
url(r'^$', views.landing, name='index'),
...
url(r'^product-index/$', 'lindshop.core.product.views.product_index', name="product_index"),
]
我product_index
观点是不带任何参数只是一个空的观点,看起来像这样:
def product_index(request):
return TemplateResponse(request, "index.html")
现在...当我打电话
reverse('lindshop.core.product.views.product_index')
我得到一个NoReverseMatch
错误。但是,如果我将url(r'^product-index/$'...)
放在demostore/demostore.urls.py中,则reverse()
可以正常工作。但这不是我要找的,我想在我的自定义应用程序的urls.py
中保留所有URL路由。
不要尝试。使用显式名称。无论如何,不推荐在URL中使用字符串视图路径。 –