执行导入操作
from django.conf.urls import patterns, include, url
出现如下报错:
ImportError: cannot import name 'patterns'
原因是实际使用的新版Django已经移除patterns,经百度后将原代码
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'superlists.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)), )
修改为:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = ['',
# Examples:
url(r'^$', 'lists.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
# url(r'^admin/', include(admin.site.urls))
]
执行出现如下报错
TypeError: view must be a callable or a list/tuple in the case of include().
修改代码为:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = ['',
# Examples:
url(r'^$', include('lists.views.home_page'), name='home'),
# url(r'^blog/', include('blog.urls')),
# url(r'^admin/', include(admin.site.urls))
再执行出现如下报错
ModuleNotFoundError: No module named 'lists.views.home_page'; 'lists.views' is not a package
导入lists目录下views中的home_page,并作如下修改
from django.conf.urls import include, url
from django.contrib import admin
from lists.views import home_page
urlpatterns = ['',
# Examples:
url(r'^$', home_page, name='home'),
# url(r'^blog/', include('blog.urls')),
# url(r'^admin/', include(admin.site.urls))
]
再执行出现如下报错
Creating test database for alias 'default'...
SystemCheckError: System check identified some issues:
ERRORS:
?: (urls.E004) Your URL pattern '' is invalid. Ensure that urlpatterns is a list of url() instances.
HINT: Try removing the string ''. The list of urlpatterns should not have a prefix string as the first element.
System check identified 1 issue (0 silenced).
这是因为在urlpatterns中多写了 [ ‘’,] 导致的,去除[ '' ]后在执行便运行正常