Django的BUG:ImportError: cannot import name 'patterns'

在学习Docker容器与容器云第2版时出现的BUG:
第二章,搭建第一个Hello World程序,运行:

python manage.py makemigrations

时报错:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.4/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.4/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.4/site-packages/django/core/management/base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.4/site-packages/django/core/management/base.py", line 342, in execute
    self.check()
  File "/usr/local/lib/python3.4/site-packages/django/core/management/base.py", line 374, in check
    include_deployment_checks=include_deployment_checks,
  File "/usr/local/lib/python3.4/site-packages/django/core/management/base.py", line 361, in _run_checks
    return checks.run_checks(**kwargs)
  File "/usr/local/lib/python3.4/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python3.4/site-packages/django/core/checks/urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "/usr/local/lib/python3.4/site-packages/django/core/checks/urls.py", line 24, in check_resolver
    for pattern in resolver.url_patterns:
  File "/usr/local/lib/python3.4/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.4/site-packages/django/urls/resolvers.py", line 313, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/local/lib/python3.4/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.4/site-packages/django/urls/resolvers.py", line 306, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/local/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/usr/src/app/dockerweb/redisweb/redisweb/urls.py", line 16, in <module>
    from django.conf.urls import patterns,include,url
ImportError: cannot import name 'patterns'

错误原因:
从1.10后django后patterns被移除了,已经没有这个模块了。使用 django 1.10 以后的版本需要改用 django.conf.urls.url() 。
错误代码:

from django.conf.urls import patterns,include,url
from django.contrib import admin
from helloworld.views import hello

urlpatterns = patterns('',
	url(r'^admin/', include(admin.site.urls)),
	url(r'^helloworld$', hello),
)

改正代码:

from django.conf.urls import url,include
from django.contrib import admin
from helloworld.views import hello

urlpatterns = [
	url(r'^admin/', include(admin.site.urls)),
	url(r'^helloworld$', hello),
]

Django的BUG:ImportError: cannot import name 'patterns'_第1张图片
运行效果:
在这里插入图片描述

你可能感兴趣的:(#,Django,#,Docker)