1. 程式人生 > >如何解決Django2.0專案urls名稱空間報錯

如何解決Django2.0專案urls名稱空間報錯

報錯資訊:

django.core.exceptions.ImproperlyConfigured: 
Specifying a namespace in include() without providing an app_name is not supported. 
Set the app_name attribute in the included module, 
or pass a 2-tuple containing the list of patterns and app_name instead.

報錯中已說明解決辦法,設定app_name屬性,或者傳遞一個兩個元素的元祖,包含路由匹配列表和app_name

解決辦法:

1 在app.urls中宣告app_name,與namespace對應

app_name='app'
urlpatterns = [
    url(r'^index/',views.index),
]

2 include方法中傳入元祖

urlpatterns = [
    url(r'^app/', include(('app.urls.,'app'), namespace='app')),
]