Django1.8与1.10中urlpatterns区别

python3.6 Django版本1.10

在菜鸟教程中的Django教程中点击打开链接有如下代码:

from django.conf.urls import *
from HelloWorld.view import hello

urlpatterns = patterns("",
	('^hello/$', hello),
)
运行后出现NameError:

NameError: name 'patterns' is not defined


搜索问题后发现有如下答案:

from django.conf.urls import *
改为

from django.conf.urls import patterns, url

修改后出现ImportError:

ImportError: cannot import name 'patterns'

后在此处发现答案点击打开链接

The use of patterns is deprecated in Django1.10. Therefore do not import 'patterns' and your url pattern should be as follows:

from django.conf.urls import include, url

urlpatterns=[
    url(r'^admin/', include(admin.site.urls)),
    url(........),
]
将原代码修改为:
from django.conf.urls import *
from HelloWorld.view import hello
'''
from django.conf.urls import patterns, url
'''
urlpatterns = [
	url('^hello/$', hello),
]

Run 'python manage.py migrate' to apply them.
March 05, 2017 - 22:52:20
Django version 1.10.5, using settings 'HelloWorld.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
运行成功


总结:patterns的用法在Django1.10中弃用,URLpattern 改为上述用法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值