django请求与响应数据

64 阅读1分钟

django请求与响应数据

request.GET:获取的是地址栏中的请求参数,数据格式为QueryDict(python中dict的子类),主要特点为值为列表,因为它可以传入多个值,get方法获取多个值中的最后一个,getlist方法获取整个列表;
request.POST:获取的是请求体,数据格式为QueryDict格式;
request.body:获取的是请求体,数据格式为json二进制字符串,loads出来后为字典格式;
request.META:获取的是请求头,应该是字典格式;
request.FILES:获取的是请求文件,get方法获取指定文件;

return HttpResponse("hello world")
return JsonResponse(goods_list,safe=False),如果返回非字典数据,务必设置 sale=False 才行
return JsonResponse(data,json_dumps_params={'ensure_ascii': False})
content = open("release.png","rb").read()
return HttpResponse(content,content_type="image/jpeg")
return HttpResponseRedirect("www.baidu.com")
return redirect("www.baidu.com")
return redirect( reverse("stu:user_login") ) # 路由反转,自动根据编写的视图方式名反解析成url地址