Django Rest Framework Reverse and SimpleRouter

How to use DRF reverse to call a complex url from SimpleRouter ?

My URL is in two places: one for teams and one for games:

league.urls:

 url(r'^team/', include('teams.urls')), 

team.urls:

 router = SimpleRouter() router.register(r'game', GameViewSet, 'games') 

I am trying to reverse specify the url to update the game. Based on DRR SimpleRouter, it should be "/ team / {pk} / game / {pk}"

My test calls:

 url = reverse('games-detail', args=[team.pk, game.pk]) 

But I get the following error:

  raise error, v # invalid expression error: redefinition of group name u'pk' as group 2; was group 1 
+5
source share
1 answer

YPCrumble, would you like to call a URL with great kwargs . The regular expression URL works in such a way as to handle kwargs . For example:

 # python reverse url url = reverse('games-detail', kwargs={'team_pk': 1, 'group_pk':1}) # url regex url( r'^team/(?P<team_pk>\d+)/group/(?P<group_pk>\d+)/$', view.SimpleRouterDetailView.as_view(), name='games-detail' ) 
+6
source

Source: https://habr.com/ru/post/1214515/


All Articles