django的request介绍和APIView流程分析和CBV的流程分析

首先get请求的数据都在request.GET中,request.body中没有,因为只有post请求有请求体,request.body中的数据是请求体的数据

首先,我们还是用cbv的方式来讲解

我们的实例是这样的

from django import views
class Rest_view(views.View):
    def get(self,request):
        print("get_get",request.GET)
        print("get_body",request.body)

        return HttpResponse("get")

    def post(self, request):
        print("post_post",request.POST)
        print("post_body",request.body)

        return HttpResponse("post")

  

首先我们通过postman发送get请求,我们看下发送的参数到底在哪里

 我们看下django的打印的结果,可以看到通过url的方式传递参数,在request.GET和request.body中均有数据

 下面我们用postman发送post请求,发送urlencoded数据

 我们看下django打印的结果,同样在request.POST和request.body中均有数据

下面我们看下使用postman工具,发送post请求,参数使用json格式发送数据

 我们在看下django的打印的结果,我们可以看到,在reqeust.POST中没有我们的数据,在request.body中有我们传递的json数据

从上面的测试,我们得出结论,在django中,源生的reqeust只会帮我们处理urlencoded数据,不会帮我们处理json数据,json数据只会在request.body中,不会在request.POST中

原文地址:https://www.cnblogs.com/bainianminguo/p/10434880.html