username with `james i. adams` not accepted with django routers

39 Views Asked by At

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

1

There are 1 best solutions below

3
deceze On BEST ANSWER

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 = '[^/]+'