zoukankan      html  css  js  c++  java
  • django 获取用户提交的数据 文件 表单

    templates:

    <div>
        <form action="/detail" method="post" enctype="multipart/form-data">
            <p><span>性别=</span>
                男:<input type="radio" name="gender" value="male">
                女:<input type="radio" name="gender" value="female" checked="checked">
            </p>
            <p><span>爱好=</span>
                香蕉:<input type="checkbox" name="favor" value="banana">
                苹果:<input type="checkbox" name="favor" value="apple">
            </p>
            <select name="area" multiple>
                <option value="bj">北京</option>
                <option value="sh">上海</option>
                <option value="gz">广州</option>
            </select>
            <input type="file" name="file" />
            <input type="submit" value="提交" />
        </form>
    </div>
    

    views:

    def detail(request):
        print(request.POST.get('gender'))
        print(request.POST.getlist('favor'))
        print(request.POST.getlist('area'))
        obj = request.FILES.get('file')  #上传文件是用files获取,是一个对象
        print(obj,type(obj))
        import os
        file_path = os.path.join('upload','1.png')
        f = open(file_path,'wb')
        for i in obj.chunks():   #chunks方法是一点点获取上传的文件内容
            f.write(i)
        f.close()
        return render(request,'detail.html')
    

      

  • 相关阅读:
    The Water Problem(排序)
    Alisha’s Party(队列)
    The Hardest Problem Ever(字符串)
    火烧赤壁
    Jquery操作一遍过
    Django之认证系统
    Mysql漂流系列(一):MySQL的执行流程
    RESTful架构&简单使用Django rest framework
    git&github快速掌握
    jsonp突破浏览器同源策略
  • 原文地址:https://www.cnblogs.com/alex-hrg/p/9670057.html
Copyright © 2011-2022 走看看