首页 > 代码库 > ImportError: cannot import name patterns

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(........),
]


这个特性在1.9就声明了deprecated. 1.10正式移除了。使用 django 1.10 需要改用 django.conf.urls.url() 示范代码:

1
2
3
4
5
6
7
8
9
10
from django.conf.urls import url
 
from import views
 
urlpatterns = [
    url(r‘^articles/2003/$‘, views.special_case_2003),
    url(r‘^articles/([0-9]{4})/$‘, views.year_archive),
    url(r‘^articles/([0-9]{4})/([0-9]{2})/$‘, views.month_archive),
    url(r‘^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$‘, views.article_detail),
]
 

ImportError: cannot import name patterns