一、问题出现:

在使用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. Mac安装mysql8.0.12

    ···shell 下载 wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.12-macos10.13-x86_64.tar.gz ...

  2. Mac 显示sudo: pip: command not found

    Mac显示sudo: pip: command not found mac在安装完pip模块后,使用pip命令会提示sudo: pip: command not found moyanzhudeMac ...

  3. npm 设置地址

    -- 查看当前地址: npm config get registry https://registry.npmjs.org/ npm config get disturl undefined -- 设 ...

  4. CGI浏览器与服务器的交互

    一直在做项目,跟着写前端后端,却没有思考一个问题:前端和后端为什么能够进行通信?为什么能够将HTML页面的内容传输给后台,然后又将结果反馈给前端? 寒假偶尔看到了这个问题,也解决了我的疑惑,这是基于C ...

  5. DP(动态规划)

    http://www.hawstein.com/posts/dp-novice-to-advanced.html https://www.topcoder.com/community/data-sci ...

  6. redis 中用正则找key

    获取 redis 中所有的 key 可用使用 *. redis 127.0.0.1:6379> KEYS * 1) "w3c3" 2) "w3c1" 3) ...

  7. np.clip截取函数

    np.clip截取函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me 将范围外的数强制转化为范围内的数 def clip(a, a_min, a_max, out=None): 将数组a中的 ...

  8. VMware ESXI6.0服务器安装

    1.制作一个ESXI6.0的系统安装盘 2.服务器启动后加载VMware ESXi 6.0的ISO文件,开始安装. 3.ESXi引导装入程序,VMware ESXi引导过程,在屏幕上方显示的版本号.内 ...

  9. 数学:莫比乌斯反演-GCD计数

    Luogu3455:莫比乌斯反演进行GCD计数 莫比乌斯反演就是用来解决这一类问题的,通常f函数是要求的那个,F函数是显然的 这样利用F的结果就可以推出来f的结果 在计算结果的时候整除分快儿一下就可以 ...

  10. prefab内容分析

    写在前面: 当前使用的unity版本:5.3.7p4. 如果打开prefab文件是乱码: 把editer的asset Srialization改为Force Text即可. 一.什么是Prefab P ...