Python 属性错误:';RegexURLPattern';对象没有属性'_回调&x27;

Python 属性错误:';RegexURLPattern';对象没有属性'_回调&x27;,python,django,django-rest-framework,Python,Django,Django Rest Framework,我是python的新手。我使用了本教程,但对RegexURLPattern有一个问题。 问题的完整堆栈跟踪: Unhandled exception in thread started by <function check_errors. <locals>.wrapper at 0x103c8cf28> Traceback (most recent call last): File "/Library/Frameworks/Python.framework/V

我是python的新手。我使用了本教程,但对RegexURLPattern有一个问题。 问题的完整堆栈跟踪:

 Unhandled exception in thread started by <function check_errors.  
 <locals>.wrapper at 0x103c8cf28>
 Traceback (most recent call last):
 File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.10.dev20151224130822-py3.5.egg/django/utils/autoreload.py", line 226, in wrapper
 fn(*args, **kwargs)
 File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.10.dev20151224130822-py3.5.egg/django/core/management/commands/runserver.py", line 116, in inner_run
 self.check(display_num_errors=True)
 File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.10.dev20151224130822-py3.5.egg/django/core/management/base.py", line 366, in check
 include_deployment_checks=include_deployment_checks,
 File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.10.dev20151224130822-py3.5.egg/django/core/checks/registry.py", line 75, in run_checks
 new_errors = check(app_configs=app_configs)
 File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.10.dev20151224130822-py3.5.egg/django/core/checks/urls.py", line 10, in check_url_config
 return check_resolver(resolver)
 File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.10.dev20151224130822-py3.5.egg/django/core/checks/urls.py", line 19, in check_resolver
 for pattern in resolver.url_patterns:
 File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.10.dev20151224130822-py3.5.egg/django/utils/functional.py", line 35, in __get__
 res = instance.__dict__[self.name] = self.func(instance)
 File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.10.dev20151224130822-py3.5.egg/django/core/urlresolvers.py", line 379, in url_patterns
 patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
 File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.10.dev20151224130822-py3.5.egg/django/utils/functional.py", line 35, in __get__
 res = instance.__dict__[self.name] = self.func(instance)
 File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.10.dev20151224130822-py3.5.egg/django/core/urlresolvers.py", line 372, in urlconf_module
 return import_module(self.urlconf_name)
 File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/importlib/__init__.py", line 126, in import_module
 return _bootstrap._gcd_import(name[level:], package, level)
 File "<frozen importlib._bootstrap>", line 986, in _gcd_import
 File "<frozen importlib._bootstrap>", line 969, in _find_and_load
 File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
 File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
 File "<frozen importlib._bootstrap_external>", line 662, in exec_module
 File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
 File "/Users/igor/tutorial/tutorial/tutorial/urls.py", line 28, in <module>
 url(r'^', include(router.urls)),
 File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/rest_framework/routers.py", line 79, in urls
 self._urls = self.get_urls()
 File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/rest_framework/routers.py", line 321, in get_urls
 urls = format_suffix_patterns(urls)
 File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/rest_framework/urlpatterns.py", line 64, in format_suffix_patterns
 return apply_suffix_patterns(urlpatterns, suffix_pattern, suffix_required)
 File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/rest_framework/urlpatterns.py", line 27, in apply_suffix_patterns
 view = urlpattern._callback or urlpattern._callback_str
 AttributeError: 'RegexURLPattern' object has no attribute '_callback'

我做错了什么?请提供帮助…

看起来您的项目布局与快速启动不同

from quickstart import views
应该是:

from tutorial.quickstart import views

您正在使用Django的开发版本。DRF尚未兼容。您应该安装Django 1.8.x或1.9.x

在我的例子中,似乎没有安装DRF。我运行了pip安装djangorestframework,我的“runserver”命令运行得很好。

我在Django 1.10和1.11上也有同样的问题。 解决方案:将djangorestframework更新至最新版本

pip install -U djangorestframework

那是疯狂的虫子。我浪费了两天的周末才意识到它必须安装特定的版本:

  • Django 1.11(我的上一个版本是1.11.3)
  • djangorestframework 3.7
  • python 3.6.5

祝你们好运

如果我像您所说的那样编辑这一行,我会遇到另一个错误:导入错误:没有名为“tutorial.quickstart”的模块正如我所说,您的快速启动布局与文档不同。请确保在创建快速启动应用程序时您在tutorial目录中。/Users/user/tutorial/tutorial/quickstart请显示完整的回溯。您使用的是哪个Django和DRF版本?更新的堆栈跟踪…Django版本-1.10 DRF-我不知道。我用pip安装djangorestframework安装它只是为了参考:从restframework3.4.0开始,Django 1.10就受支持。
pip install -U djangorestframework