标签:stat char upload path_info 字符串 body rom ken cti
print("返回用户访问的url,但是不包括域名",request.path_info)
print("返回请求的方法,全大写",request.method)
print("返回HTTPde GET参数的类的字典对象",request.GET)
print("返回HTTPde POST参数的类的字典对象", request.POST)
print("请求体",request.body)
结果如下:

首先看下form表单该如何写
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="/app1/upload/" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" placeholder="上传文件" name="file">
<input type="submit" value="提交">
</form>
</body>
</html>
重点是这里

然后看下后端视图函数,用request.FILES方法获取上传的文件的对象
def upload(request):
method = request.method.lower()
if method == "get":
return render(request,"upload.html")
else:
# print(dir(request))
file_name = request.FILES["file"].name
name = request.FILES.get("file").name
size = request.FILES.get("file").size
print("---------->",dir(request.FILES.get("file")))
print(name,size)
import os
new_file_path = os.path.join("static","upload",name)
with open(new_file_path,"wb") as f:
for chunks in request.FILES.get("file").chunks():
f.write(chunks)
return HttpResponse(file_name)
def js(request):
ret = {"name":"xiaocui","age":23}
# 视图函数返回json字符串,有下面三种方法
# 方法1
import json
return HttpResponse(json.dumps(ret))
# 方法2
from django.http import JsonResponse
return JsonResponse(ret)
# 默认情况下JsonResponse只能转换字典为js的字符串,如果是列表是转换不成jsson的字符串d的,如果要转换列表为js字符串则使用下面的方法
# 方法3
from django.http import JsonResponse
return JsonResponse(ret,safe=False)
# 告诉JsonResponse不要为我做安全监察
标签:stat char upload path_info 字符串 body rom ken cti
原文地址:https://www.cnblogs.com/bainianminguo/p/9649453.html