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

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参数?

最佳答案

这应该工作:

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

关于django-rest-framework - django rest框架在APIView中找到URL kwarg,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39670713/

10-14 05:21