Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: No module named rest_framework.authtoken

I'm using django rest-framework's, (DRF), token authentication in my project to create tokens when a user is created. Everything works great until I add this line from the DRF docs:

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

to create an endpoint that returns the token for a user after correctly posting the username and password.

It throws this error:

ImportError: No module named rest_framework.authtoken

This is strange because DRF is fine with out this line, so it must be included in my PYTHONPATH.

I've also run python manage.py syncdb and the appropriate migrations.

Any idea what could be wrong?

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

Full stack trace:

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
like image 778
agconti Avatar asked Apr 23 '14 17:04

agconti


1 Answers

Check out the last response here. Instead of including the whole view as a string, import 'obtain_auth_token' first and then just refer to that.

from rest_framework.authtoken.views import obtain_auth_token

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

Further update from agconti:

This issue stems from using:

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

because of the views prefix. If you want to use api-token-auth/ this way you must change it to below or use the solution provided by Alex:

urlpatterns = patterns('',
    ...
    url(r'^api-token-auth/', 'rest_framework.authtoken.views.obtain_auth_token'),
    ...
)
like image 180
Alex Avatar answered Sep 24 '22 11:09

Alex