码迷,mamicode.com
首页 > 移动开发 > 详细

Django2.0异常:Specifying a namespace in include() without providing an app_name is not supported.

时间:2019-07-14 15:24:43      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:long   font   sans   order   ble   ecif   container   dynamic   testcase   

Django2.0异常:Specifying a namespace in include() without providing an app_name is not supported.

欢迎使用 小书匠(xiaoshujiang)编辑器,您可以通过设置里的修改模板来改变新建文章的内容。

Django2.0异常:Specifying a namespace in include() without providing an app_name is not supported.编辑器

在CRM项目权限分配管理中
用django设置好了URL后报错。URL代码为

from django.conf.urls import url,include
from django.contrib import admin

urlpatterns = [
    url(r‘^admin/‘, admin.site.urls),
    url(r‘^rbac/‘, include(‘rbac.urls‘,namespace=‘rbac‘)),
    url(r‘^‘, include(‘web.urls‘)),
]

源码为

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)

作者:masserd
来源:CSDN
原文:https://blog.csdn.net/zoulonglong/article/details/79612973
版权声明:本文为博主原创文章,转载请附上博文链接!

从include()函数可以看出来,这个函数有两个参数,一个arg,一个namespace,我在代码中也是两个参数,但是异常中提示了,没有提供app_name,还提示需要传入一个两元元组,从第六行代码,可以看出来,arg就是那个元组,且给app_name赋值了

urlconf_module, app_name = arg

经过一番面向百度debug,修改后代码为

from django.conf.urls import url,include
from django.contrib import admin

urlpatterns = [
    url(r‘^admin/‘, admin.site.urls),
    url(r‘^rbac/‘, include((‘rbac.urls‘,‘rbac‘),namespace=‘rbac‘)),
    url(r‘^‘, include(‘web.urls‘)),
]

成功解决问题

Django2.0异常:Specifying a namespace in include() without providing an app_name is not supported.

标签:long   font   sans   order   ble   ecif   container   dynamic   testcase   

原文地址:https://www.cnblogs.com/bruce-blogs/p/11184144.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!