4

I want to upload excel file and save that file to specific location in django without creating model for that file.

I tried here my forms.py file

class UploadFileForm(forms.Form):

    file  = forms.FileField(label='Select a file',
        help_text='max. 42 megabytes')

my views.py

import xlrd  
from property.forms import UploadFileForm

def excel(request):

    if request.method == 'POST':

        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = handle_uploaded_file(request.FILES['file'])
            print newdoc
            print "you in"
            newdoc.save()

            return HttpResponseRedirect(reverse('upload.views.excel'))
    else:
        form = UploadFileForm() # A empty, unbound form
    #documents = Document.objects.all()
    return render_to_response('property/list.html',{'form': form},context_instance=RequestContext(request))

def handle_uploaded_file(f):
    destination = open('media/file/sheet.xls', 'wb+')
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()

So while trying this i am getting the error.

IOError at /property/excel/
[Errno 2] No such file or directory: 'media/file/sheet.xls'
Request Method: POST
Request URL:    http://127.0.0.1:8000/property/excel/
Django Version: 1.5
Exception Type: IOError
Exception Value:    
[Errno 2] No such file or directory: 'media/file/sheet.xls'
Exception Location: D:\Django_workspace\6thMarch\dtz\property\views.py in handle_uploaded_file, line 785

Please help me out for this, There is a problem in handle_uploaded_file() function.

1
  • At least initially this is a valid question. Commented Sep 18, 2014 at 17:19

1 Answer 1

1

If you use open (as in open('path', 'wb+'), then you need to use FULL path.

What you could do is:

from django.conf import settings
destination = open(settings.MEDIA_ROOT + filename, 'wb+')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.