Django - Where are the params stored on a PUT/DELETE request?

前端 未结 7 1188
[愿得一人]
[愿得一人] 2020-11-30 00:17

I\'d like to follow the RESTful pattern for my new django project, and I\'d like to know where the parameters are when a PUT/DELETE request is made.

As far as I know

7条回答
  •  野性不改
    2020-11-30 00:58

    My approach was to override the dispatch function so I can set a variable from the body data using QueryDict()

    from django.contrib.auth.mixins import LoginRequiredMixin
    from django.http import QueryDict
    from django.views.generic import View
    
    
    class GenericView(View):
    
        def dispatch(self, request, *args, **kwargs):
            if request.method.lower() in self.http_method_names:
                handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
    
                # if we have a request with potential body data utilize QueryDict()
                if request.method.lower() in ['post', 'put', 'patch']:
                    self.request_body_data = {k: v[0] if len(v)==1 else v for k, v in QueryDict(request.body).lists()}
            else:
                handler = self.http_method_not_allowed
            return handler(request, *args, **kwargs)
    
    
    class ObjectDetailView(LoginRequiredMixin, GenericView):
    
        def put(self, request, object_id):
            print("updating object", object_id)
            print(self.request_body_data)
    
        def patch(self, request, object_id):
            print("updating object", object_id)
            print(self.request_body_data)
    
    

提交回复
热议问题