ImportError:无法导入名称' URLPattern'

时间:2018-06-19 01:09:29

标签: django django-rest-framework

我正在使用django 1.11和django rest version 3。 我使用drf嵌套路由器。对于像/abc/{id}/xyz/这样的makin路径,其中abc和xyz是来自AbcViewSet和XyzViewSet的两个视图。

urls.py

from rest_framework_nested import routers
    router = routers.SimpleRouter()
    router.register(r'abc',AbcViewset)
    abc_router = routers.NestedSimpleRouter(router,r'abc', lookup='abc')
    abc_router.register(r'xyz', XyzViewset, base_name='abc-xyz')

    urlpatterns = [
        url(r'', include(router.urls)),
    ]

我得到了以下追溯。我不知道代码发生了什么,错误究竟是什么?

Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7fb79f505a60>
Traceback (most recent call last):
  File "/home/johan/.local/lib/python3.6/site-packages/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/home/johan/.local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
    self.check(display_num_errors=True)
  File "/home//.local/lib/python3.6/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/johan/.local/lib/python3.6/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/johan/.local/lib/python3.6/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/johan/.local/lib/python3.6/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/home/johan/.local/lib/python3.6/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/home/johan/.local/lib/python3.6/site-packages/django/urls/resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "/home/johan/.local/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/johan/.local/lib/python3.6/site-packages/django/urls/resolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/johan/.local/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/johan/.local/lib/python3.6/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/johan/Documents/Abc/backend/abc/urls.py", line 24, in <module>
    from rest_framework.documentation import include_docs_urls
  File "/home/johan/.local/lib/python3.6/site-packages/rest_framework/documentation.py", line 6, in <module>
    from rest_framework.schemas import SchemaGenerator, get_schema_view
  File "/home/johan/.local/lib/python3.6/site-packages/rest_framework/schemas/__init__.py", line 25, in <module>
    from .generators import SchemaGenerator
  File "/home/johan/.local/lib/python3.6/site-packages/rest_framework/schemas/generators.py", line 18, in <module>
    from rest_framework.compat import (
ImportError: cannot import name 'URLPattern'

如何解决此错误?

0 个答案:

没有答案