我在使用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中使用字符串視圖路徑。 –