一、問題出現:
在使用Django2.0,配置全局URL時,希望指向某個APP的URL,配置如下:
from django.contrib import admin
from django.conf.urls import url,include
urlpatterns = [
url(r'^admin/', admin.site.urls),
# 配置users應用的URL
url(r'^users/', include('users.urls', namespace='users')),
]
運行該項目,會出現報錯:
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.
而使用Django1.0,則會可以正常運行。
二、原因分析:
在Django1.0中include的源碼是:
def include(arg, namespace=None, app_name=None):
if app_name and not namespace:
raise ValueError('Must specify a namespace if specifying app_name.')
if app_name:
warnings.warn(
'The app_name argument to django.conf.urls.include() is deprecated. '
'Set the app_name in the included URLconf instead.',
RemovedInDjango20Warning, stacklevel=2
)
是可以接收app_name參數的,而在Django2.0中:
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參數的,那么如何將參數傳給app_name呢;
既然2.0是1.0的升級版,肯定有方法解決這個問題,查看官方文檔中對include的介紹:
-
..........
include()
also accepts as an argument either an iterable that returns URL patterns or a 2-tuple containing such iterable plus the names of the application namespaces.Parameters:
文檔中指明是可以傳入module的,傳給變量arg的,沒有傳給app_name,文中提示可以傳入2個參數的元組給arg,源碼中有:
if isinstance(arg, tuple):
# Callable returning a namespace hint.
try:
urlconf_module, app_name = arg
說明第二個元組的參數會傳給app_name,找到問題解決的方法。
三、解決方案
解決問題將URL配置換成:
url(r'^users/', include(('users.urls', 'users'), namespace='users')),
可正常啟動。
四、補充
在官方文檔中提示:
Changed in Django 2.0:
In older versions, this function is located in django.conf.urls
. The old location still works for backwards compatibility.
URL中的include方法之前1.0在 django.conf.urls 下,而2.0在 django.urls 下,但是之前位置還可以引用,說明在2.0中以下兩種方式引用都是可以的:
from django.urls import include
from django.conf.urls import include