Specifying a namespace in include() without providing an app_name is not supported.
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.index),
path('login_action/', views.login_action),
path('event_manage/', views.event_manage),
path('accounts/login/', views.index),
path('search_name/', views.search_name),
path('guest_manage/', views.guest_manage),
re_path('^sign_index/(?P<eid>[0-9]+)/', views.sign_index),
re_path('^sign_index_action/(?P<eid>[0-9]+)/', views.sign_index_action),
path('logout/', views.logout),
path('api/', include('sign.urls', namespace='sign'))
]
错误:
/Users/sujianlin/DjangoDemo/venv/bin/python /Users/sujianlin/DjangoDemo/manage.py runserver 8000
Performing system checks…
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py”, line 932, in _bootstrap_inner
self.run()
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py”, line 870, in run
self._target(*self._args, **self._kwargs)
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/utils/autoreload.py”, line 53, in wrapper
fn(*args, **kwargs)
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/management/commands/runserver.py”, line 117, in inner_run
self.check(display_num_errors=True)
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/management/base.py”, line 392, in check
all_issues = self._run_checks(
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/management/base.py”, line 382, in _run_checks
return checks.run_checks(**kwargs)
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/checks/registry.py”, line 72, in run_checks
new_errors = check(app_configs=app_configs)
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/checks/urls.py”, line 13, in check_url_config
return check_resolver(resolver)
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/checks/urls.py”, line 23, in check_resolver
return check_method()
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/urls/resolvers.py”, line 407, in check
for pattern in self.url_patterns:
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/utils/functional.py”, line 48, in get
res = instance.dict[self.name] = self.func(instance)
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/urls/resolvers.py”, line 588, in url_patterns
patterns = getattr(self.urlconf_module, “urlpatterns”, self.urlconf_module)
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/utils/functional.py”, line 48, in get
res = instance.dict[self.name] = self.func(instance)
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/urls/resolvers.py”, line 581, in urlconf_module
return import_module(self.urlconf_name)
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/init.py”, line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “
查找错误,conf.py
源代码:
def include(arg, namespace=None):
app_name = None
if isinstance(arg, tuple):
# Callable returning a namespace hint.
try:
urlconf_module, app_name = arg
except ValueError:
if namespace:
raise ImproperlyConfigured(
'Cannot override the namespace for a dynamic module that '
'provides a namespace.'
)
raise ImproperlyConfigured(
'Passing a %d-tuple to include() is not supported. Pass a '
'2-tuple containing the list of patterns and app_name, and '
'provide the namespace argument to include() instead.' % len(arg)
)
else:
# No namespace hint - use manually provided namespace.
urlconf_module = arg
if isinstance(urlconf_module, str):
urlconf_module = import_module(urlconf_module)
patterns = getattr(urlconf_module, 'urlpatterns', urlconf_module)
app_name = getattr(urlconf_module, 'app_name', app_name)
if namespace and not app_name:
raise 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.',
)
namespace = namespace or app_name
# Make sure the patterns can be iterated through (without this, some
# testcases will break).
if isinstance(patterns, (list, tuple)):
for url_pattern in patterns:
pattern = getattr(url_pattern, 'pattern', None)
if isinstance(pattern, LocalePrefixPattern):
raise ImproperlyConfigured(
'Using i18n_patterns in an included URLconf is not allowed.'
)
return (urlconf_module, app_name, namespace)
从include()函数可以看出来,这个函数有两个参数,一个arg,一个namespace,我在代码中也是两个参数,但是异常中提示了,没有提供app_name,还提示需要传入一个两元元组,从第六行代码
urlconf_module, app_name = arg
可以看出来,arg就是那个元组,且给app_name赋值了,所以我们这里修改代码为:
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.index),
path('login_action/', views.login_action),
path('event_manage/', views.event_manage),
path('accounts/login/', views.index),
path('search_name/', views.search_name),
path('guest_manage/', views.guest_manage),
re_path('^sign_index/(?P<eid>[0-9]+)/', views.sign_index),
re_path('^sign_index_action/(?P<eid>[0-9]+)/', views.sign_index_action),
path('logout/', views.logout),
path('api/', include(('sign.urls', 'sign'), namespace='sign'))
]