Is possible to put the TokenObtainPairView (django-rest-frakmework-simplejwt) endpoint in Api Root on DRF?
Asked Answered
C

1

6

Consider the code below (in urls.py):

router = DefaultRouter()
router.register('my-endpoint', MyViewSet, basename='mybasename')
urlpatterns = [
    path('api/v1/', include(router.urls)),
    path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), 
]

This code is valid but the 2 endpoints (TokenObtainPairView and TokenRefreshView) not appear in the Root API (DRF)

Then, I tried to register both endpoints of django-rest-frakmework-simplejwt on the router:

router = DefaultRouter()
router.register('my-endpoint', MyViewSet, basename='mybasename'),
router.register('api/token/', TokenObtainPairView.as_view(), basename='token_obtain_pair')
router.register('api/token/refresh/', TokenRefreshView.as_view(), basename='token_refresh')

# Other way:
# router.register('api/token/', TokenObtainPairView, basename='token_obtain_pair')
# router.register('api/token/refresh/', TokenRefreshView, basename='token_refresh')

urlpatterns = [
    path('api/v1/', include(router.urls))
]

And then when I run, the following error occurs:

File "/home/sidon/dev/boticario-teste/boticashback/boticashback/urls.py", line 32, in <module>
    path('api/', include(router.urls)),
File "/home/sidon/miniconda3/envs/botcash/lib/python3.8/site-packages/rest_framework/routers.py", line 78, in urls
    self._urls = self.get_urls()
File "/home/sidon/miniconda3/envs/botcash/lib/python3.8/site-packages/rest_framework/routers.py", line 339, in get_urls
    urls = super().get_urls()
File "/home/sidon/miniconda3/envs/botcash/lib/python3.8/site-packages/rest_framework/routers.py", line 237, in get_urls
    routes = self.get_routes(viewset)
File "/home/sidon/miniconda3/envs/botcash/lib/python3.8/site-packages/rest_framework/routers.py", line 153, in get_routes
    extra_actions = viewset.get_extra_actions()
AttributeError: type object 'TokenObtainPairView' has no attribute 'get_extra_actions'
Cohune answered 17/2, 2020 at 14:46 Comment(2)
Did you figure this out? Trying to do the same thingCrocodile
@MarcoFernandes, Found a solution for this? I am facing same issue.Padnag
H
0

Do not use router for token paths. Use them as separate url paths, and it will work. Use your view set in the router path, and put the token paths in urlpatterns.

Hectoliter answered 22/11, 2022 at 6:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.