django没有模型上传文件

Vam*_*hna 7 python django

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

我目前的代码如下:

在我的模板/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>
Run Code Online (Sandbox Code Playgroud)

在我的帮助视图中:

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()
Run Code Online (Sandbox Code Playgroud)

在对文件数据进行操作时会抛出以下错误.

Exception Value:    
"Key 'myfile' not found in <MultiValueDict: {}>"
Run Code Online (Sandbox Code Playgroud)

请指教.我理想情况下不喜欢使用任何数据库,因为我想要的是将文本/图像导入静态位置.

Dan*_*man 7

你错过了标签中的enctype="multipart/form-data"属性<form>.

编辑

handle_uploaded_file您调用的参数f是UploadedFile的一个实例,根据文档name属性,它可能会根据名称更改目标.或者您可以查看内容本身并以此方式确定.


Moa*_*ghi 5

上传文件时不必使用模型。你可以用这个

<form method="post" enctype="multipart/form-data">
    <input type="file" name="sentFile" />
    <input type="submit" name="submit" value="Upload" />
</form>
Run Code Online (Sandbox Code Playgroud)

并在意见中

def myview(request):
    f = request.FILES['sentFile'] # here you get the files needed
    print f.name
Run Code Online (Sandbox Code Playgroud)