django 2.1 錯誤 specifying namespace include providing app supported 欄目 Python 简体版
原文   原文鏈接

錯誤信息:html

Specifying a namespace in include() without providing an app_name is not supporteddjango

  File "I:\xx\urls.py", line 22, in <module>
    url('', include('system.urls', namespace='system-urls')),
  File "I:\xx\venv\lib\site-packages\django\urls\conf.py", line 39, in include
    '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

urlpatterns = [
    url('', include('apps.urls', namespace='apps-urls')),
    ]

 

背景:ide

django == 2.1

錯誤意思爲:post

django.core.exceptions.ImproperlyConfigured:不支持在include()中指定名稱空間而不提供app_name。 在包含的模塊中設置app_name屬性,或者傳遞包含模式列表和app_name的2元組。this

源碼文件:url

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, 當namespace不爲空時,arg參數必須是一個2元組,除了urlpatterns不能爲空以外,app_name也必須填寫。spa

解決辦法:code

urlpatterns = [
    url('', include(('apps.urls', 'apps'), namespace='apps-urls')),
    ]
 
相關文章
相關標籤/搜索
每日一句
    每一个你不满意的现在,都有一个你没有努力的曾经。
本站公眾號
   歡迎關注本站公眾號,獲取更多信息