I think (i am not sure if its correct) I make it work like this. In my main 
urls.py

api_v1 =[                                                              
    url(r'^api/v1/', 
        include('my_project.apps.agreements.api_v1.urls')),  
    url(r'^api/v1/',
    include('my_project.apps.authentication.api_v1.urls'))   ]

urlpatterns = [
    url(r'', include((api_v1,'v1'), namespace="v1")),]

If instead, in the url patterns I include like this:

urlpatterns = [
    url(r'', include(api_v1, namespace="v1")),]

I have the following error: 'Specifying a namespace in include() without 
providing an app_name ' django.core.exceptions.ImproperlyConfigured: 
Specifying a namespace in include() without providing an app_name is not 
supported. Set the app_name attribute in the included module, or pass a 
2-tuple containing the list of patterns and app_name instead.

The thing is that 'v1' as the second argument, the "app_name", in fact its 
not an app name, I just put it there to not have the error....

Also now, the reverse I have to make it like this (it works):

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

And not like I wanted that is:

reverse('v1:authentication-user-detail' . 


El viernes, 21 de diciembre de 2018, 12:17:19 (UTC+11), Gonzalo Amadio 
escribió:
>
> 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