上传文件的demo
####################################
from django.shortcuts import render from django.views.decorators.csrf import csrf_exempt @csrf_exempt def qigeming(request): if request.method == "POST": file_obj = request.FILES.get("kouge") with open(file_obj.name, "wb") as f: for line in file_obj.chunks(): f.write(line) return render(request, "qigeming.html")
######################################
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/qigeming/" method="post" enctype="multipart/form-data"> <p>写点东西:<input type="file" name="kouge"></p> <p><input type="submit" value="提交头皮发麻"></p> </form> </body> </html>
###########################################
注意点: 1,<form action="/qigeming/" method="post" enctype="multipart/form-data"> 2,file_obj = request.FILES.get("kouge")
###########################################
###############################################
技术改变命运