一、问题出现:

在使用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. CodeSmith自己动手写模板

    CodeSmith学习笔记------ 1.新建一个Code Smith Generator Template(C sharp) 2.一些常见标签的解释: ①外部变量: <%@ Property ...

  2. bzoj4807 車

    题目大意: Description 众所周知,車是中国象棋中最厉害的一子之一,它能吃到同一行或同一列中的其他棋子.車跟車显然不能在一起打 起来,于是rly一天又借来了许多许多的車在棋盘上摆了起来……他 ...

  3. eos 源码net_plugin分析

    1 net_plugin_impl::connect(connection_ptr c) 函数用于解析地址,内部异步回调async_resolve async_resolve 传递了lambda表达式 ...

  4. CentOS 7.4 java验证码乱码的问题

     转载阿里云 摘要: 新服务器配置发布网站 配置后程序顺利启动在登录时发现验证码无法识别显示出了图片,但是字是乱码 初步估计应该是字体问题 ssh登录服务器查看默认字体 #fc-match msam1 ...

  5. NodeJS API Process全局对象

    Process 全局对象,可以在代码中的任何位置访问此对象,使用process对象可以截获进程的异常.退出等事件,也可以获取进程的当前目录.环境变量.内存占用等信息,还可以执行进程退出.工作目录切换等 ...

  6. docker 时区设置

    今天查问题的时候发现 在对时间 格式化为 时间戳的时候,time.mktime(time.strptime('20170609-15:00:00','%Y%m%d-%H:%M:%S')) 发现测试环境 ...

  7. 说说SQL Server的数据类型

    以SQL Server为例,SQL Server的数据类型总共有33种,归纳为一下类别: 精确数字 Unicode字符串 近似数字 二进制字符串 日期和时间 其他数据类型 字符串   1.数字数据类型 ...

  8. Knowledge Tracing -- 基于贝叶斯的学生知识点追踪(BKT)

    目前,教育领域通过引入人工智能的技术,使得在线的教学系统成为了智能教学系统(ITS),ITS不同与以往的MOOC形式的课程.ITS能够个性化的为学生制定有效的 学习路径,通过根据学生的答题情况追踪学生 ...

  9. C语言复习---获取最大公约数(辗转相除法和更相减损法)

    源自:百度百科 辗转相除法 辗转相除法:辗转相除法是求两个自然数的最大公约数的一种方法,也叫欧几里德算法. 例如,求(,): ∵ ÷=(余319) ∴(,)=(,): ∵ ÷=(余58) ∴(,)=( ...

  10. python---django中文件上传

    服务端: def upload(req): if req.method == "GET": return render(req, 'upload.html') else: prin ...