django rest框架在APIView中找到url kwarg

时间:2016-09-23 23:01:34

标签: django-rest-framework

我有一个看起来像这样的网址:

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

和APIView:

class ClientProfileAddressView(APIView):

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

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

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

putget中,我需要访问id url kwarg,第一个更新对象,第二个更新它。如何在这些方法中访问url参数?

1 个答案:

答案 0 :(得分:7)

这应该有效:

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')