[Fixed]-Django Rest Framework using dot in url


First, it’s ok to use the . (dot) in urls. See
Can . (period) be part of the path part of an URL?

Second, the problem wasn’t in the format option but in the Regex expression used to catch the Primary Key, which excludes the . (dot) and / (slash) per default.

(?P<pk>[^/.]+)  <--- This excludes the dots in the IP Address

The regex can be overrided in the ViewSet with lookup_value_regex. This is the new ViewSet that solves the problem:

class VTEViewSet(viewsets.ModelViewSet):
    lookup_value_regex = '[0-9.]+'      #Just add this line & change your Regex if needed
    queryset = models.VTE.objects.all()
    serializer_class = VTESerializer
    permission_classes = (permissions.AllowAny,)

Leave a comment