Django上传没有模型的文件 [英] django uploading files without model

查看:48
本文介绍了Django上传没有模型的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想上传没有模型的文件(文本/图像)(仅使用视图和模板).理想情况下,我想读取或写入位置.

I would like to upload files (text/images) without a model ( just using views and templates). I would ideally like to read or write to location.

我当前的代码如下:

在我的模板/foo/help/UploadFileContent.html

In my template /foo/help/UploadFileContent.html

<!doctype html>
<html>
<body>
<link rel="stylesheet" href="{{STATIC_URL}}/stylesheets/jquery-ui.css">
<div class="main-content">
    <div class="container">


        <div class="container">
            <div class="row">

                <div class="box">
                    <div class="box-content">
                        <div class="col-md-12">
                            <form method="post" id="fileupload" action="/helpsubmitfilecontent/" accept-charset="utf-8" class="fill-up">
                                {% csrf_token %}
                                <div class="row">
                                    <div class="col-lg-4">

                                        <ul class="padded separate-sections">
                                            <li>
                                                <input type="text" name="name" id="name" placeholder="Name"/>
                                            </li>

                                            <li>
                                                <textarea name="content" id="content"
                                                          placeholder="Help Contents" style="height: 250px;width: 700px"></textarea>
                                            </li>
                                           <li>
                                                <input type="file" name="myfile" id="myfile" />

                                           </li>
                                        </ul>

                                        <div class="form-actions">
                                            <button type="submit" class="btn btn-blue">Submit</button>
                                        </div>

                                    </div>
                                </div>
                            </form>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>

</body>

<script>
    $('#fileupload').submit(function(evnt){
       if($('#name').val().length <=0){
           $('#name').attr('style','border-color:red');
           evnt.preventDefault();
       }
       if($('#content').val().length <=0){
           $('#content').attr('style','border-color:red');
           evnt.preventDefault();
       }
    });

</script>
</html>

在我的帮助视图中:

def fileupload(request):

    return responsewrapper('pages/foo/help/UploadFileContent.html', locals(),request)

def submitfilecontent(request):
    myhash = dict()
    myhash['name'] = request.POST['name'] # works
    myhash['filedata'] = request.POST['content'] # works
    handle_uploaded_file(request.FILES['myfile']) # error throws up here.
    return HttpResponseRedirect("/successupload")

def handle_uploaded_file(f):
    destination = open('/home/foo/name.txt', 'wb+')
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()

对文件数据进行操作时会引发以下错误.

The following error throws up while operating on the file data.

Exception Value:    
"Key 'myfile' not found in <MultiValueDict: {}>"

请咨询.理想情况下,我不希望使用任何数据库,因为我想要的只是将文本/图像导入到静态位置.

Please advice. I would ideally not like to use any databases, as all i want is to import text/images to static location.

推荐答案

您在< form> 中缺少了 enctype ="multipart/form-data" 属性code>标签.

You're missing the enctype="multipart/form-data" attribute in your <form> tag.

修改

handle_uploaded_file 的参数(您称为 f )是UploadedFile的一个实例,根据

The argument to handle_uploaded_file, which you've called f, is an instance of UploadedFile, which according to the docs has a name attribute, so you could presumably change the destination depending on the name. Or you could look at the contents themselves and determine it that way.

这篇关于Django上传没有模型的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆