Django Rest Framework如何在可浏览的API上发布数据 [英] Django Rest Framework how to post data on the browsable API
问题描述
class ProcessBill(APIView):
def post(self,request )
bill_data = request.data
print(bill_data)
返回响应(只是一个测试,status = status.HTTP_200_OK)
当我访问指向此视图的url时,我可以从服务器的响应获取可浏览的api视图的rest_framework 方法不允许
这是可以理解的,因为我没有设置一个 def get()
方法。但是...如何POST数据?我期待着某种形式的某种形式。
编辑
这是一个关于可浏览API如何查找我的截图,它是西班牙文。 视图
与上文中写的相同,但是以西班牙语显示。正如你可以看到的:没有POST数据的形式:/。
由于你是新的,我建议你使用通用视图,它将节省大量的时间,使您的生活更轻松:
serializer_class = ProcessBillSerializer $ b $($)$ b def create(self,request,* args,** kwargs):
bill_data = request.data
print(bill_data)
return bill_data
我将推荐通过 DRF教程以不同的方式实现您的端点和一些高级功能,如一般视图, Permissions 等。
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 :/ .
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.
这篇关于Django Rest Framework如何在可浏览的API上发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!