django 2.0 中URL的include方法使用分析


一、問題出現:

在使用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 – URLconf module (or module name)
  • namespace (str) – Instance namespace for the URL entries being included
  • pattern_list – Iterable of path() and/or re_path() instances.
  • app_namespace (str) – Application namespace for the URL entries being included

文檔中指明是可以傳入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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM