首页 > 代码库 > python+django 更改了urls.py 之后runserver报错的解决办法

python+django 更改了urls.py 之后runserver报错的解决办法

新手学python+django时,更改了urls.py 为:

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns(‘‘,
    # Examples:
    # url(r‘^$‘, ‘mysite.views.home‘, name=‘home‘),
    # url(r‘^blog/‘, include(‘blog.urls‘)),

    url(r‘^admin/‘, admin.site.urls),
    url(r‘^$‘, ‘mysite.views.first_page‘),
)

 

进入mysite,启动服务器:

  python manage.py runserver 8000

却报错:ImportError: cannot import name patterns

这是因为:从1.10后django后patterns被移除了,已经没有这个模块了。

第一步,我先将

from django.conf.urls import patterns, include, url

改成

from django.conf.urls import include, url

再运行试试看,还是会报错:TypeError: view must be a callable or a list/tuple in the case of include() 

第二步,既然报这个错,那我就加上include()呗,于是将

 

url(r‘^admin/‘, admin.site.urls),

 

 

url(r‘^$‘, ‘mysite.views.first_page‘),

改成

url(r‘^admin/‘, include(admin.site.urls)),

url(r‘^$‘, include(‘mysite.views.first_page‘)),

再次运行试试看,又报错了:ImportError:Not module names‘mysite.views.first_page‘;‘mysite.views‘is not package

咋办勒?

第三步,只能去FQgoogle了,最好的解决办法是这样的,删除include包并导入mysite.views包:

 

from django.conf.urls import url
from django.contrib import admin
from mysite import views

urlpatterns = [
url(r‘^admin/‘, admin.site.urls),
url(r‘^$‘,views.first_page)
]

 

python+django 更改了urls.py 之后runserver报错的解决办法