Search code examples
djangourldjango-authenticationdjoser

Django djoser has two same urls


I'm reading about Djoser framework for Django.

I found that there are two url patters to include, both of which are the same:

urlpatterns = [
    (...),
    url(r'^auth/', include('djoser.urls')),
    url(r'^auth/', include('djoser.urls.jwt')),
]

I thought that Django always takes the first match

How is it possible that the second path is picked?


Solution

  • The urlpatterns you give are:

    urlpatterns = [
        (...),
        url(r'^auth/', include('djoser.urls')),
        url(r'^auth/', include('djoser.urls.jwt')),
    ]
    

    From the usage of the function include you can see that those patterns are simply including other patterns. See Including other URLconfs [Django docs].

    If we look at the the urls from 'djoser.urls' (GitHub) they basically look like:

    router = DefaultRouter()
    router.register("users", views.UserViewSet)
    
    User = get_user_model()
    
    urlpatterns = router.urls
    

    So basically with the usage of include this ends up generating a bunch of url patterns that look like auth/users/...

    Moving to the patterns in 'djoser.urls.jwt' (GitHub):

    urlpatterns = [
        re_path(r"^jwt/create/?", views.TokenObtainPairView.as_view(), name="jwt-create"),
        re_path(r"^jwt/refresh/?", views.TokenRefreshView.as_view(), name="jwt-refresh"),
        re_path(r"^jwt/verify/?", views.TokenVerifyView.as_view(), name="jwt-verify"),
    ]
    

    Which basically end up as auth/jwt/create, etc.

    Looking at this the patterns are obviously different so there is no need to be concerned.