templates:
<div> <form action="/detail" method="post" enctype="multipart/form-data"> <p><span>性别=</span> 男:<input type="radio" name="gender" value="male"> 女:<input type="radio" name="gender" value="female" checked="checked"> </p> <p><span>爱好=</span> 香蕉:<input type="checkbox" name="favor" value="banana"> 苹果:<input type="checkbox" name="favor" value="apple"> </p> <select name="area" multiple> <option value="bj">北京</option> <option value="sh">上海</option> <option value="gz">广州</option> </select> <input type="file" name="file" /> <input type="submit" value="提交" /> </form> </div>
views:
def detail(request): print(request.POST.get('gender')) print(request.POST.getlist('favor')) print(request.POST.getlist('area')) obj = request.FILES.get('file') #上传文件是用files获取,是一个对象 print(obj,type(obj)) import os file_path = os.path.join('upload','1.png') f = open(file_path,'wb') for i in obj.chunks(): #chunks方法是一点点获取上传的文件内容 f.write(i) f.close() return render(request,'detail.html')