ImportError: cannot import name 'patterns'

执行导入操作

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中多写了 [ ‘’,] 导致的,去除[ '' ]后在执行便运行正常

转载于:https://my.oschina.net/u/2431775/blog/997325

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值