【发布时间】:2019-06-14 23:41:54
【问题描述】:
我很难理解 app_name 和命名空间之间的联系。
考虑项目级别的 urls.py
from django.urls import path, include
urlpatterns = [
path('blog/', include('blog.urls', namespace='blog')),
]
考虑应用级别(博客)urls.py
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
path('', views.post_list, name='post_list'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/', views.post_detail, name='post_detail'),
]
如果我注释掉 app_name,我会得到以下信息。
'Specifying a namespace in include() without providing an app_name '
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 = 'x'
我已阅读文档,但仍然没有点击。有人可以告诉我 app_name 和命名空间是如何/为什么连接的,为什么它们可以有不同的字符串值吗?手动设置 app_name 不是多余的吗?
【问题讨论】:
标签: python django python-3.x django-urls