Python Forum

Full Version: Django views : positional argument
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is probably something simple but it has eluded me so far so good.

My view looks like the following, currently:

#request - /ip/?ip=172.69.89.90

class IPView(APIView):
    permission_classes = (IsAuthenticated,)

    def get(self, request, format=None):
        ip = int(ipaddress.IPv4Address(request.query_params['ip']))
        #do something with the ip and give a response
       
While the above works, passing ip as a positional argument and not as query parameter fails to match any url. My understanding is that the below should also work but it does not because the request fails to match any url

#request /api/172.89.90.25

class IPView(APIView):
    permission_classes = (IsAuthenticated,)

    def get(self, request, ip, format=None):
        #do something with the ip and give a response
My urls file looks like:

from django.urls import path

from .views import IPView, CustomAuthToken

urlpatterns = [
    path('api-token-auth/', CustomAuthToken.as_view()),
    path('ip/', IPView.as_view()),
]
Is it possible someone can spot where my issue lies? Am I making any mistakes that I didn't notice yet?