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 改为上述用法。