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')