一、问题出现:

在使用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

django 2.0 中URL的include方法使用分析的更多相关文章

  1. yii2.0中url重写实现方法

    在yii框架里有前台和后台页面,举例前台url重写. 控制器与路由 控制器以Controller作为后缀,继承自yii\web\Controller; 动作以action作为前缀,public访问修饰 ...

  2. Django 3.0 中连接mysql 8.0,可以不使用pymysql ,升级Mysqlclient即可

    python 中,连接mysql一般都推荐用pymysql ,而且在django中,网上的教程都是这么连接mysql的. import pymysql pymysql.install_as_MySQL ...

  3. Django 3.0中不推荐使用的及已经删除的功能

    3.0中不推荐使用的功能 django.utils.encoding.force_text()和smart_text()的别名被弃用.如果您的代码支持Python 2,smart_str()并且 fo ...

  4. AntiXSS v4.0中Sanitizer.GetSafeHtmlFragment等方法将部分汉字编码为乱码的解决方案

    AntiXSS v4.0中Sanitizer.GetSafeHtmlFragment等方法将部分汉字编码为乱码的解决方案 以下代码为asp.net环境下,c#语言编写的解决方案.数据用Dictiona ...

  5. Django2.0中URL的路由机制

    路由是关联url及其处理函数关系的过程.Django的url路由配置在settings.py文件中ROOT_URLCONF变量指定全局路由文件名称. Django的路由都写在urls.py文件中的ur ...

  6. Django 2.0 新款URL配置详解

    Django2.0发布后,很多人都拥抱变化,加入了2的行列.但是和1.11相比,2.0在url的使用方面发生了很大的变化,下面介绍一下: 一.实例 先看一个例子: from django.urls i ...

  7. AFNetworking 3.0中调用[AFHTTPSessionManager manager]方法导致内存泄漏的解决办法

    在使用AFNetworking3.0框架,使用Instruments检查Leaks时,检测到1000多个内存泄漏的地方,定位到 [AFHTTPSessionManager manager] 语句中,几 ...

  8. vue3.0中的双向数据绑定方法

    熟悉vue的人都知道在vue2.x之前都是使用object.defineProperty来实现双向数据绑定的 而在vue3.0中这个方法被取代了 1. 为什么要替换Object.definePrope ...

  9. django urls.py 中的name 使用方法

    使用场景: 当我们在url的时候,一般情况下都是使用很明确的url地址.如在网页里面使用<a href="/login">登录</a>.像这样的链接有很 多 ...

随机推荐

  1. git开发部署流程

    git的分支操作 https://blog.csdn.net/QH_JAVA/article/details/77853605 Git 开发部署流程 采用业界成熟方案 Git Flow 分支方式进行开 ...

  2. Eclipse Neon安装指导

    [下载] 前往Eclipse官网:http://www.eclipse.org/,点击DOWNLOAD: 进入下载页面后,会显示如下下载界面: 找到 Get Eclipse Neon,然后点击下面的” ...

  3. SQL Server 查询性能优化——覆盖索引

    覆盖索引又可以称为索引覆盖. 解释一: 就是select的数据列只用从索引中就能够取得,不必从数据表中读取,换句话说查询列要被所使用的索引覆盖. 解释二: 索引是高效找到行的一个方法,当能通过检索索引 ...

  4. Spark记录-Spark on mesos配置

    1.安装mesos #用centos6的源yum安装 # rpm -Uvh http://repos.mesosphere.io/el/6/noarch/RPMS/mesosphere-el-repo ...

  5. js实现表单提交submit(),onsubmit

    通常表单的提交有两种方式,一是直接通过html的form提交,代码如下: <form action="" method="" id="forms ...

  6. [iOS]Xcode处理过时方法的警告

    ####强迫症的福利, 有的时候, 我们特别讨厌Xcode中的代码警告, 以下就是遇到各种警告的时候的处理方法:(后续会一直更新) 产生警告的原因: 某些方法废弃了, 会产生警告! 样式: 处理方法: ...

  7. 第13月第12天 Constraints priority

    1.Constraints priority 将evInputView的高度约束的priority设为750,evInputView的inputTextView如果不设高度约束,那么高度就是defau ...

  8. HTML5+CSS把footer固定在底部

    在刚开始给网页写footer的时候,我们会碰到一个让人烦躁的问题:当页面内容太少时,footer显示在了页面中间,这是我们不希望出现的,我们希望它能够永远呆在底部,不管网页的内容是多还是少.下面的代码 ...

  9. 产品排序(2015 年北大自招夏令营) (与栈相关的区间DP)

    题面: \(solution:\) 又是一道\(DP\)的好题啊!状态并不明显,需要仔细分析,而且还结合了栈的特性! 做这一类题,只要出题人有点理想,一定会在栈的性质上做点文章,所以我们尽量围绕栈的性 ...

  10. sql server查询某年某月有多少天

    sql语句如下: ),) date from (),,)+'-01' day) t1, ( ) t2 ),) ),,)+'%' 查询结果如下: 2017年2月共有28天,查询出28条记录.