template html(模板文件):

<form enctype="multipart/form-data" method="POST" action="/address/upload/">
<input type="file" name="file" />
<br />
<input type="submit" value="上传文件" />
</form>

a、自定义上传(建议用自定义的)

def index(request):
if request.method == 'POST': obj = request.FILES.get('file')
import os
ppp = os.path.join('static','img',obj.name) f = open(ppp, 'wb')
for chunk in obj.chunks():
f.write(chunk)
f.close()
return HttpResponse(request,'上传成功')
# return render(request, 'file.html')
else:
return render(request,'index.html')
            head_img = request.FILES.get('file')
print(head_img,"我是file")
tags = request.POST.get('tags', None) # obj = request.FILES.get('file')
import os
print(head_img.chunks(),"我是名字")
Statics = os.path.dirname(os.path.dirname(__file__))
user_img_file_path = os.path.join(Statics,"Statics",'Down_image',username)
print(user_img_file_path)
if not os.path.exists(user_img_file_path): os.mkdir(user_img_file_path) img_file_path = os.path.join(user_img_file_path,head_img.name) f = open(img_file_path, 'wb')
for chunk in head_img.chunks():
f.write(chunk)
f.close()
return HttpResponse('上传成功')

b、Form上传文件实例

class FileForm(forms.Form):
ExcelFile = forms.FileField()

form

from django.db import models

class UploadFile(models.Model):
userid = models.CharField(max_length = )
file = models.FileField(upload_to = './upload/')
date = models.DateTimeField(auto_now_add=True)

models

def UploadFile(request):
uf = AssetForm.FileForm(request.POST,request.FILES)
if uf.is_valid():
upload = models.UploadFile()
upload.userid =
upload.file = uf.cleaned_data['ExcelFile']
upload.save() print upload.file

django 的文件上传的更多相关文章

  1. python Django之文件上传

    python Django之文件上传 使用Django框架进行文件上传共分为俩种方式 一.方式一 通过form表单进行文件上传 #=================================== ...

  2. day56:django:csrf_token&文件上传

    目录 1.csrf介绍 2.django实现csrf_token认证 3.django实现文件上传 csrf介绍 什么是csrf? csrf:跨站请求伪造.攻击者通过HTTP请求将数据传送到服务器,从 ...

  3. Django(十七)文件上传

    http://www.cnblogs.com/wupeiqi/articles/5703697.html - 文件上传        - 普通上传        - 自定义页面上传按钮        ...

  4. 框架----Django之文件上传

    一.文件上传 1. 浏览器访问 http://127.0.0.1:8000/f1/ http://127.0.0.1:8000/f2/ 2. urls from django.conf.urls im ...

  5. Django的文件上传以及预览、存储

    思路: 文件上传通过前端的input标签,input设置display:none属性. 内容显示需要让前端通过<img>标签读取图片内容,可以通过<label>标签连接< ...

  6. Django实现文件上传功能

    文件上传 关注公众号"轻松学编程"了解更多. 1.创建上传文件夹 在static文件夹下创建uploads用于存储接收上传的文件 在settings中配置,MEDIA_ROOT=o ...

  7. Django普通文件上传

    前端代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  8. Django处理文件上传File Uploads

    HttpRequest.FILES 表单上传的文件对象存储在类字典对象request.FILES中,表单格式需为multipart/form-data <form enctype="m ...

  9. Django之文件上传

    一.form表单上传文件 注意: 1.form上需要加enctype="multipart/form-data" 2.form提交的地址需要以/结尾 def form_file(r ...

随机推荐

  1. Servlet之Filter详解

    参考文献:http://www.cnblogs.com/zlbx/p/4888312.html Filter,过滤器,顾名思义,即是对数据等的过滤,预处理过程.为什么要引入过滤器呢?在平常访问网站的时 ...

  2. hdu 1020 Encoding

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Given a ...

  3. apk支持html video控制 ,是播放状态就暂停,暂停状态就播放

    apk支持html video控制 <!DOCTYPE html> <html> <body> <div style="text-align:cen ...

  4. Array subscript is not an integer

    字典的字母写成大写了,也不能查成出来,没有报没有这个字典,反而报这个错……找了好久

  5. Code First 迁移

    https://msdn.microsoft.com/zh-cn/data/jj591621 Data Access and Storage > 学习 > Entity Framework ...

  6. Autofac.Integration.Web分析

    using System; using System.Web; using Autofac.Core.Lifetime; namespace Autofac.Integration.Web { /// ...

  7. axure新手入门教程

    首先做个声明:此次教程里为了快速完成,借用了一些网上已有教程的图文,不是剽窃,只图方便.另外,因为汉化版本可能功能名称等略有差别,请自行理解. 名词解释: 线框图:一般就是指产品原型,比如:把线框图尽 ...

  8. 在虚机中安装CentOS

    摘要 最近看到.net core 1发布的内容,也想尝试着在lunix上跑一圈.linux这方面的知识一直都没怎么接触过,只在工作中见同事操作过,看到满屏幕的命令行,感觉非常的高大上,趁着现在赶紧学习 ...

  9. div+css使多行文字垂直居中?

    1.单行文字: 设置height = line-height; 多行文字: 设置 padding, 自己要计算一下? vertical-align: 可以设置垂直居中, 但是只是针对本身就具有 ali ...

  10. 网页引导:jQuery插件实现的页面功能介绍引导页效果

    现在很多网站不仅是介绍,更多的是有一些功能,怎么样让客户快速的知道网站有哪些功能呢?这里pagewalkthrough.js插件能帮我们实现,它是一个轻量级的jQuery插件,它可以帮助我们创建一个遮 ...