Search code examples
pythondjangodjango-rest-frameworkauth-token

AuthToken in Django Rest FrameWork: non_field_errors


I m trying to integrate token based authentication in DRF(Django version 1.10) but when I hit api-token-auth/ using {"username":"test","password":"123456789"} as mentioned in the doc it is required to return me the Token but I m getting

{
    "non_field_errors": [
        "Unable to log in with provided credentials."
    ]
}

I have used rest_framework.authtoken in my installed apps also token is getting generated once the user is registered and save in authtoken_token table .

Also in my urls.py of root I m using

urlpatterns += [
    url(r'^api-token-auth/', authviews.obtain_auth_token),
]

Any help would be appreciated. Also attaching the code

urls.py

urlpatterns += [
    url(r'^api-token-auth/', authviews.obtain_auth_token),
]

settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework.authtoken',
    'users'
]

users/urls.py


from rest_framework.routers import DefaultRouter
from . import views as user_views
from django.conf.urls import url ,include


router = DefaultRouter()
router.register(r'user', user_views.UserViewSet,base_name="user")
urlpatterns = router.urls


Solution

  • You are probably not hashing your password and saving it as it is. In your view, you should save password like this.

    user = User.objects.create(usename='test', first_name='first_name', email='[email protected]')
    user.set_password('password')
    user.save()
    

    user.set_password will hash password.