Django Rest Framework how to post data on the brow

2019-07-15 09:09发布

Im kind of new to Django Rest Framework. I know it is possible to post data using the Browsable API, I just don't know how. I have this simple view:

class ProcessBill(APIView):

    def post(self, request):

        bill_data = request.data
        print(bill_data)

        return Response("just a test", status=status.HTTP_200_OK)

When I go to the url that points to this view, I get the rest_framework browsable api view with the response from the server method not allowed which is understandable cause I am not setting a def get() method. But ... how can I POST the data? I was expecting a form of some kind somewhere.

EDIT This is a screenshot of how the browsable API looks for me, it is in spanish. The view is the same I wrote above but in spanish. As you can see ... no form for POST data :/ .

enter image description here

1条回答
爷、活的狠高调
2楼-- · 2019-07-15 09:48

Since you are new I will recommend you to use Generic views, it will save you lot of time and make your life easier:

class ProcessBillListCreateApiView(generics.ListCreateAPIView):
    model = ProcessBill
    queryset = ProcessBill.objects.all()
    serializer_class = ProcessBillSerializer

    def create(self, request, *args, **kwargs):
        bill_data = request.data
        print(bill_data)
        return bill_data

I will recommend to go also through DRF Tutorial to the different way to implement your endpoint and some advanced feature like Generic views, Permessions, etc.

查看更多
登录 后发表回答