码迷,mamicode.com
首页 > Web开发 > 详细

django上传文件

时间:2017-02-04 11:03:40      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:print   put   head   open   lin   type   meta   oct   ret   

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

 

django上传文件

标签:print   put   head   open   lin   type   meta   oct   ret   

原文地址:http://www.cnblogs.com/liruixin/p/6364178.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!