1. 程式人生 > >Specifying a namespace in include() without providing an app_name

Specifying a namespace in include() without providing an app_name

原始碼:

根路徑檔案:

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'myblog/', include('blog.urls', namespace='blog'))
]

子路徑檔案:

urlpatterns = [
    url(r'blog/$', views.blog),
    url(r'article/(?P<article_id>[0-9]+)$', views.article_page, name='article_page')
]

根據錯誤提醒,在include方法裡面沒有提供APP名稱

一下提供兩種解決方法:

(1)在根路徑下的include()加入APP名稱,我這裡是blog

(2)在子路徑下加入

 app_name = 'blog'(加入這句)

urlpatterns = [

    url(r'blog/$', views.blog), url(r'article/(?P<article_id>[0-9]+)$', views.article_page, name='article_page')

]