原地址:点击打开链接
运行环境
win7
Django 2.0
python 3.7
在网页项目中使用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')), ]
- 在app/urls.py中对应url
from django.urls import path from .views import index urlpatterns = [path('',index,name='index'), ]
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.
意思为:
在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')),
]
方法2:在app/urls.py中修改
from django.urls import path
from .views import index
app_name='common'
urlpatterns = [path('',index,name='index'),
]