關於Django Specifying a namespace in include() without providing an app_name
阿新 • • 發佈:2018-12-09
執行環境
PyCharm 2017.1
Django 2.0.1
python 3.6.1
在網頁專案中使用include()方法
專案目錄中同時存在app/urls.py和proj/urls.py
在proj/urls.py使用include方法
from django.urls import path,include
from app import urls as app_url
urlpatterns = [
path('', include(app_url, namespace='common')),
]
1
2
3
4
5
在app/urls.py中對應url
from django.urls import path
from .views import index
urlpatterns = [
path('',index,name='index'),
]
1
2
3
4
5
runserver發生錯誤
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.
1
2
3
4
意思為:
在include方法裡面指定namespace卻不提供app_name是不允許的。
在包含的模組裡設定app_name變數,或者在include方法裡面提供app_name引數。
解決方法
方法1:在proj/urls.py中修改
from django.urls import path,include
from app import urls as app_url
urlpatterns = [
path('', include((common_url,'common'), namespace='common' )),
]
1
2
3
4
5
方法2:在app/urls.py中修改
from django.urls import path
from .views import index
app_name='common'
urlpatterns = [
path('',index,name='index'),
]