Reputation: 33521
I am learning both Django and the Django REST framework. I had this error before and fixed it. Now, this problem rears its head again.
This is the error I get when trying to get an auth token:
'module' object has no attribute 'views'
and this is my urls.py
:
from django.conf.urls import include, url
import rest_framework
from rest_framework import authtoken
from . import views
urlpatterns = [
url(r'^games/$', views.GameList.as_view()),
url(r'^games/(?P<pk>[0-9]+)/$', views.GameDetail.as_view()),
url(r'^users/$', views.UserList.as_view()),
url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view()),
url(r'^api-token-auth/', authtoken.views.obtain_auth_token),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
Somehow it can't find authtoken.views
. Annoying thing is, this worked fine until I restarted with manage.py runserver
.
Upvotes: 3
Views: 6255
Reputation: 3610
I was facing same error, but I realized that below thing.
You need to add rest_framework.authtoken
to your INSTALLED_APPS
,
And don't forget to python manage.py migrate
Upvotes: 2
Reputation: 11906
The reason it doesn't work - authtoken
is a package - when you import it, it does not contain what you want -
>>> from rest_framework import authtoken
>>> dir(authtoken)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']
You can see that authtoken
does not contain anything useful. However the view you are interested in is actually inside the views
module.
So we can first change the import to:
from rest_framework.authtoken import views as authviews
Then use it in the urlconf:
url(r'^api-token-auth/', authviews.obtain_auth_token),
Upvotes: 10