[Fixed]-Django Rest Framework reverse and SimpleRouter

23👍

YPCrumble, you would want to call the URL with distinct kwargs. The URL regex works in a way to handle kwargs. So 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'
)

-1👍

The issue seems to be related to the use of named groups in URL patterns. Using the args parameter in the reverse function, it’s causing a conflict with the named groups in URL pattern.

To address this issue, we can use the kwargs parameter in the reverse function to pass the values for the named groups. Here’s example:

url = reverse('games-detail', kwargs={'team_pk': team.pk, 'pk': game.pk})

For more comprehension:

  1. reverse in rest_framework
  2. router in rest_framework

Leave a comment