首页 > Web开发 > 详细

django上传文件

时间:2019-11-10 14:45:38      阅读:92      评论:0      收藏:0      [点我收藏+]

1、另外创建一个app02

技术分享图片

 

 

2、在app02/views.py创建 upload视图

技术分享图片

 

 

def upload(request):
    if request.method == "POST":
        print( request.FILES.get("myfile"))
        print(type( request.FILES.get("myfile") ))
        file_obj = request.FILES.get("myfile")
        file_name = file_obj.name
        with open( file_name, "wb" ) as f:
            for line in file_obj:
                f.write(line)
    return render(request, upload.html)

 

3、在app02/templates里面创建模板 upload.html

技术分享图片

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1 > 文件上传</h1>
<form action="" method="post" enctype="multipart/form-data">
    <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token}}" >
    <p> <input type="file" name="myfile"> </p>
    <input type="submit">
</form>
</body>
</html>

 

4、配置视图对应的路由

在app02/urls.py里面配置路由

技术分享图片

 

 在工程下的urls.py配置路由分发

技术分享图片

 

 

5、配置app02/templates对应的settings设置

技术分享图片

 

 

6、访问url:  http://127.0.0.1:8080/app02/upload/

技术分享图片

 

 提交后,文件会被后台接收:

技术分享图片

 

 

 

 

django上传文件

原文:https://www.cnblogs.com/harryTree/p/11829669.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!