Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django uploading files without model

Tags:

python

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.

My current code is as below:

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>

In my help views:

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.

like image 854
Vamsi Krishna Avatar asked Feb 19 '14 13:02

Vamsi Krishna


2 Answers

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

Edit

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.

like image 132
Daniel Roseman Avatar answered Oct 04 '22 11:10

Daniel Roseman


you don't have to use models when you are uploading files. you can use this

<form method="post" enctype="multipart/form-data">
    <input type="file" name="sentFile" />
    <input type="submit" name="submit" value="Upload" />
</form>

and in the views

def myview(request):
    f = request.FILES['sentFile'] # here you get the files needed
    print f.name
like image 27
Moayyad Yaghi Avatar answered Oct 04 '22 12:10

Moayyad Yaghi