无法导入名称模式 - django

Posted

技术标签:

【中文标题】无法导入名称模式 - django【英文标题】:cannot import name patterns - django 【发布时间】:2017-07-31 09:06:06 【问题描述】:

我正在使用 Visual Studio 2013 和 Python Tools for VS 2013 来开始使用 Django 网站。

它给出了以下错误,

完整追溯

Performing system checks...

Traceback (most recent call last):
  File "c:\users\my_username\documents\visual studio 2013\Projects\DjangoWebProject
1\DjangoWebProject1\manage.py", line 17, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
367, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 294,
 in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\runserver.
py", line 58, in execute
    super(Command, self).execute(*args, **options)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 345,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\runserver.
py", line 97, in handle
    self.run(**options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\runserver.
py", line 108, in run
    self.inner_run(None, **options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\runserver.
py", line 121, in inner_run
    self.check(display_num_errors=True)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 374,
 in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 361,
 in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Python27\lib\site-packages\django\core\checks\registry.py", line 81,
in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Python27\lib\site-packages\django\core\checks\urls.py", line 14, in c
heck_url_config
    return check_resolver(resolver)
  File "C:\Python27\lib\site-packages\django\core\checks\urls.py", line 24, in c
heck_resolver
    for pattern in resolver.url_patterns:
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 35, in _
_get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python27\lib\site-packages\django\urls\resolvers.py", line 313, in ur
l_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 35, in _
_get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python27\lib\site-packages\django\urls\resolvers.py", line 306, in ur
lconf_module
    return import_module(self.urlconf_name)
  File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "c:\users\my_username\documents\visual studio 2013\Projects\DjangoWebProject
1\DjangoWebProject1\DjangoWebProject1\urls.py", line 6, in <module>
    from django.conf.urls import patterns, url
ImportError: cannot import name patterns
Press any key to continue . . .

我该如何解决这个问题?

【问题讨论】:

首先,尝试在调试模式下运行它,并将完整的回溯附加到您的问题。 【参考方案1】:

patterns 在 Django 1.10 中被删除。相反,请使用常规列表:

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^articles/2003/$', views.special_case_2003),
    url(r'^articles/([0-9]4)/$', views.year_archive),
    url(r'^articles/([0-9]4)/([0-9]2)/$', views.month_archive),
    url(r'^articles/([0-9]4)/([0-9]2)/([0-9]+)/$', views.article_detail),
]

请参阅docs 了解更多信息。

【讨论】:

文件 "c:\users\my_usrname\documents\visual studio 2013\Projects\DjangoWebProject1\DjangoWebProject1\DjangoWebProject1\urls.py",第 3 行,在 中来自 .导入视图 ImportError: cannot import name views Press any key to continue 。 . . 请开始一个新问题。正确附加您的回溯。如果这解决了问题,请接受答案。 我已经应用了您的解决方案,但没有奏效。让你知道那是这两个 cmets 的唯一目的 cannot import name views 表示它无法导入名为 views.py 的模块(Python 文件)。那是一个不同的问题。堆栈溢出不是调试器。

以上是关于无法导入名称模式 - django的主要内容,如果未能解决你的问题,请参考以下文章

Django,ImportError:无法导入名称 Celery,可能的循环导入?

Django 无法导入名称视图

Django - URL 路由问题(无法导入名称'urls')

Django:无法导入名称路径

Django:ImportError:无法导入名称“用户”

无法导入名称六 Django-CMS