views:

36

answers:

2

I am trying to upload text files to a specified location, but the file is uploading empty, in the root(not in the location i specified).

in models:

course = models.FileField(help_text=('Upload a course (max %s kilobytes)' %settings.MAX_COURSE_UPLOAD_SIZE),upload_to='cfolder/',blank=True)

in forms:

def handle_uploaded_file(f):
    destination = open('Cfolder')
    for chunk in f.chunks():
        destination.write(chunk)       
    destination.close()

in views:

def save_course(request, classname):
   classroom = Classroom.objects.get(classname = classname)
   if request.method == 'POST':
        form = CoursesForm(request.POST, request.FILES)
        if form.is_valid():
           handle_uploaded_file(request.FILES['course'])
           new_obj = form.save(commit=False)
           new_obj.creator = request.user
           new_obj.classroom = classroom
           new_obj.save()
           return HttpResponseRedirect('.')    
   else:
           form = CoursesForm()     
   return render_to_response('courses/new_course.html', {
           'form': form,
           }, 
          context_instance=RequestContext(request))  

i guess my error is in the handle_uploaded_file method. How should i modify it to be working fine? Thanks!

+3  A: 

Did you remember to put enctype="multipart/form-data" in your HTML <form> tag?

Oli
+1  A: 

what is the purpose of calling the handle_uploaded_file function ??

by default form.save will save the file in the upload_to directory

Ashok