ImportError:没有名为rest_framework.authtoken的模块

时间:2014-04-23 17:20:00

标签: python django django-rest-framework

我在项目中使用django rest-framework,(DRF),令牌身份验证来创建用户时创建令牌。一切都很有效,直到我从DRF文档中添加这一行:

url(r'^api-token-auth/', 'rest_framework.authtoken.views.obtain_auth_token'),

创建一个端点,在正确发布用户名和密码后返回用户的令牌。

它抛出了这个错误:

ImportError: No module named rest_framework.authtoken

这很奇怪,因为DRF在这条线上很好,所以它必须包含在我的PYTHONPATH中。

我还运行了python manage.py syncdb和相应的迁移。

知道可能出现什么问题吗?

Settings.py:

THIRD_PARTY_APPS = (
        'south',  # Database migration helpers:
        'crispy_forms',  # Form layouts
        'avatar',  # for user avatars
        'rest_framework', # for rest
        'django_rq', # for aysnc
        'rest_framework_swagger', # for exploring the api
        'rest_framework.authtoken',
    )
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS

完整堆栈跟踪:

Unhandled exception in thread started by <function wrapper at 0x1046096e0>
Traceback (most recent call last):
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/utils/autoreload.py", line 93, in wrapper
    fn(*args, **kwargs)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 101, in inner_run
    self.validate(display_num_errors=True)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/management/base.py", line 310, in validate
    num_errors = get_validation_errors(s, app)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/management/validation.py", line 34, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/db/models/loading.py", line 196, in get_app_errors
    self._populate()
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/db/models/loading.py", line 78, in _populate
    self.load_app(app_name)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/db/models/loading.py", line 99, in load_app
    models = import_module('%s.models' % app_name)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
    __import__(name)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/debug_toolbar/models.py", line 63, in <module>
    patch_root_urlconf()
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/debug_toolbar/models.py", line 51, in patch_root_urlconf
    reverse('djdt:render_panel')
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 480, in reverse
    app_list = resolver.app_dict[ns]
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 310, in app_dict
    self._populate()
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 273, in _populate
    for name in pattern.reverse_dict:
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 296, in reverse_dict
    self._populate()
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 285, in _populate
    lookups.appendlist(pattern.callback, (bits, p_pattern, pattern.default_args))
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 229, in callback
    self._callback = get_callable(self._callback_str)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/utils/functional.py", line 32, in wrapper
    result = func(*args)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 100, in get_callable
    not module_has_submodule(import_module(parentmod), submod)):
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
    __import__(name)
ImportError: No module named rest_framework.authtoken

1 个答案:

答案 0 :(得分:8)

查看上一个回复here。不要将整个视图包含为字符串,而是先导入'obtain_auth_token',然后再引用它。

from rest_framework.authtoken.views import obtain_auth_token

...
url(r'^api-token-auth/', obtain_auth_token),
...

agconti的进一步更新:

此问题源于使用:

urlpatterns = patterns("api.views",
    ...
    url(r'^api-token-auth/', 'rest_framework.authtoken.views.obtain_auth_token'),
    ...
)

因为视图前缀。如果您想以这种方式使用api-token-auth/,则必须将其更改为以下或使用Alex提供的解决方案:

urlpatterns = patterns('',
    ...
    url(r'^api-token-auth/', 'rest_framework.authtoken.views.obtain_auth_token'),
    ...
)