django配置url的include需要app_name:include() without providing an app_name

from django.contrib import admin
from django.conf.urls import url,include
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', include('myapp.urls', namespace='one')),
]

运行该项目,会出现报错:

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.

我的django版本是2,以下是源码

def include(arg, namespace=None):
    app_name = None
    if isinstance(arg, tuple):
        # Callable returning a namespace hint.
        try:
            urlconf_module, app_name = arg

默认app_name=None,所以我们需要自己去指定才行

这个app_name是什么呢?

app_namespace (str) – Application namespace for the URL entries being included,是你app的名字

解决方法:

解决问题将URL配置换成:

url(r'^$', include(('myapp.urls', 'app01'), namespace='one')),

其中的‘app01’是我的应用名字

同时,在app01.urls中

from django.urls import path

from .views import index

app_name='common'

urlpatterns = [ path('',index,name='index'), ]

猜你喜欢

转载自blog.csdn.net/weixin_42557907/article/details/81503017