1👍
Per the documentation, you can expose an API endpoint that takes a username/password and returns a token using rest_framework.authtoken.view.obtain_auth_token
. See the rest framework Docs for more details. You urls.py would look like this:
from .users.api.views import UserViewSet
from rest_framework.authtoken import views
router = routers.DefaultRouter()
router.register('users', UserViewSet, 'user')
urlpatterns = [
url(r'^v1/', include(router.urls)),
url(r'^v1/login, views.obtain_auth_token)
]
If you really want this url to belong to the UserViewSet
that you’ve already defined, you will need to define a detail_route
and manually call authenticate
and then generate a token for the authenticated user (if authenticate succeeds). I recommend using the first pattern I described as it’s less code/customization.
Source:stackexchange.com