首页 > 代码库 > django上传文件

django上传文件

html示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>首页</div>
<form  action="index/"  method="post" enctype="multipart/form-data" >
<input type="file" name="upload" />
<input type="submit" value="http://www.mamicode.com/提交" />
</form>
</body>
</html>

 views函数设置

def index(request):
    if request.method == "GET":
        return render(request,"index.html")
    elif request.method == "POST":
        obj = request.FILES.get("upload")
        print(obj.name)
        f = open(obj.name, "wb")
        for line in obj.chunks():
            f.write(line)
        f.close()
        return render(request,"index.html")

 

django上传文件