0

I have registered routers for user model, which has viewset that has lookup_url as username. The username james adams is accepted by the router, but getting below error for james i. adams

django.urls.exceptions.NoReverseMatch: Reverse for 'user-detail' with keyword arguments '{'username': 'james i. adam', 'version': 'v1'}' not found. 4 pattern(s) tried: ['(?P<version>(v4))/users/(?P<username>[^/.]+)/?\\.(?P<format>[a-z0-9]+)/?$', '(?P<version>(v4))/users/(?P<username>[^/.]+)/?$', '(?P<version>(v1))/users/(?P<username>[^/.]+)/?\\.(?P<format>[a-z0-9]+)/?$', '(?P<version>(v1))/users/(?P<username>[^/.]+)/?$'] 

Can someone guide me, how can I allow such username for url patterns with routers registered?

Thanks in Advance

sandeshdaundkar
  • 883
  • 1
  • 7
  • 14
  • I see you do have an answer, but more standard practice might be to generate a unique slug for the record and use that in the URL instead of the actual username. If you look at the SO URL for this question, that's exactly what they are doing there ... – michjnich Dec 15 '21 at 09:04

1 Answers1

2

As you see, the default regex for URL values excludes .:

/users/(?P<username>[^/.]+)/
                       ^

You will have to change that regex; if you're using viewsets, that's as easy as:

class UserViewSet(ModelViewSet):
    lookup_value_regex = '[^/]+'
deceze
  • 510,633
  • 85
  • 743
  • 889