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="提交" />
</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")

 

posted @ 2017-02-04 10:30  李瑞鑫  阅读(158)  评论(0编辑  收藏  举报