I want to version my api, but cannot make reverse function work.

I am following the namespace versioning schema proposed in the DRF website 
: namespaceversioning 
<https://www.django-rest-framework.org/api-guide/versioning/#namespaceversioning>

I have one app called *authentication* and inside authentication folder I 
have :

 authentication/
 |
  -- models.py, apps.py, admin.py
  -- api_v1/
     |
     -- views.py
     -- serializers.py
     -- urls.py

In my main urls.py, I've defined

urlpatterns = [
    url(r'admin/', admin.site.urls),
    url(r'^api/v1/',include
        ('tektank.apps.authentication.api_v1.urls',namespace='v1')
     ),
    url(r'^$', lambda _: redirect('http://localhost:8888/')),]

This is authentication/api_v1/urls.py

from rest_framework.routers import DefaultRouter                       
from authentication.api_v1.views import UserViewSet                    

app_name = 'authentication'                                            
router = DefaultRouter()                                               
router.register(r'users', UserViewSet, base_name='authentication-user')

urlpatterns = router.urls                                              

And when I execute

./manage.py show_urls

 /api/v1/users/ authentication.api_v1.views.UserViewSet 
v1:authentication-user-list
 /api/v1/users/<pk>/    authentication.api_v1.views.UserViewSet 
v1:authentication-user-detail

When I am trying to reverse for example from shell, I have the following 
error:

> reverse('v1:authentication-user-detail', kwargs={'pk': '5'})
NoReverseMatch: Reverse for 'authentication-user-detail' not found. 
'authentication-user-detail' is not a valid view function or pattern name.
> reverse('authentication:v1:posts-job-detail', kwargs={'pk': '5'})
NoReverseMatch: 'v1' is not a registered namespace inside 'authentication'
> reverse('v1:authentication:posts-job-detail', kwargs={'pk': '5'})
NoReverseMatch: 'authentication' is not a registered namespace inside 'v1'

BUT, if I do not put namespace='v1' in the app urls, like this:

url(r'^api/v1/',include('tektank.apps.authentication.api_v1.urls')

Then the reverse functions works

> reverse('authentication:authentication-user-detail', kwargs={'pk':5})> 
> '/api/v1/users/5/'

I think I am calling the reverse in the wrong way, or maybe some 
configuration? Because if I call the api for example via postman, the 
endpoint is working ok.

Or maybe I am having the wrong approach?

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to