ImportError: cannot import name ‘patterns’

Don’t panic in case of data tilt, teach you to easily obtain the slope of table tilt>>>

Perform the import operation

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

The following error is reported:

ImportError: cannot import name 'patterns'

The reason is that the actual use of the new version of Django has removed patterns, and the original code will be changed by Baidu

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)), )

To be amended as follows:

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))
]

The following error is reported during execution

TypeError: view must be a callable or a list/tuple in the case of include().

The modification code is as follows:

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))

The following error is reported during re execution

ModuleNotFoundError: No module named 'lists.views.home_page'; 'lists.views' is not a package

Import home from views in lists directory_ Page, with the following modifications

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))
]

The following error is reported during re execution

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).

This is caused by the addition of [‘,] in urlpatterns. After the removal of [‘], the execution will run normally

Similar Posts: