8

I have a url that looks like this:

url(r'^client_profile/address/(?P<id>.+)/$', views.ClientProfileAddressView.as_view())

And an APIView:

class ClientProfileAddressView(APIView):

    renderer_classes = (JSONRenderer,)
    permission_classes = (IsAuthenticated,)

    def put(self, request):
          ....

    def get(self, request):
          ....

In both put and get, I need to access the id url kwarg, the first one to update the object, the second to update it. How can I access the url argument in those methods?

1 Answer 1

20

This should work:

def put(self, request, *args, **kwargs):
      id = kwargs.get('id', 'Default Value if not there')

def get(self, request, *args, **kwargs):
      id = kwargs.get('id', 'Default Value if not there')
Sign up to request clarification or add additional context in comments.

3 Comments

Can you also explain where this comes from and link to the documentation where it is explained? This information is hard to find in the docs as I don't know exactly what to look for (it is not on the Class-based Views page)
As discussed here, one can get URL kwargs from the view object itself.
I think the OP, like me, was wondering how you discen this from the actual Django docs... so we can learn more related stuff...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.