1

i have the model:

class OpenCv(models.Model):
    created_by = models.ForeignKey(User, blank=True)
    first_name = models.CharField(('first name'), max_length=30, blank=True)
    last_name = models.CharField(('last name'), max_length=30, blank=True)
    url = models.URLField(verify_exists=True)
    picture = models.ImageField(help_text=('Upload an image (max %s kilobytes)' %settings.MAX_PHOTO_UPLOAD_SIZE),upload_to='jakido/avatar',blank=True, null= True)
    bio = models.CharField(('bio'), max_length=180, blank=True)
    date_birth = models.DateField(blank=True,null=True)
    domain = models.CharField(('domain'), max_length=30, blank=True, choices = domain_choices)
    specialisation = models.CharField(('specialization'), max_length=30, blank=True)
    degree = models.CharField(('degree'), max_length=30, choices = degree_choices)
    year_last_degree = models.CharField(('year last degree'), max_length=30, blank=True,choices = year_last_degree_choices)
    lyceum = models.CharField(('lyceum'), max_length=30, blank=True)
    faculty = models.ForeignKey(Faculty, blank=True,null=True)
    references = models.CharField(('references'), max_length=30, blank=True)
    workplace = models.ForeignKey(Workplace, blank=True,null=True)  

the form:

class OpencvForm(ModelForm):
class Meta:
      model = OpenCv
      fields = ['first_name','last_name','url','picture','bio','domain','specialisation','degree','year_last_degree','lyceum','references']

and the view:

 def save_opencv(request):
   if request.method == 'POST':
    form = OpencvForm(request.POST, request.FILES)
   # if 'picture' in request.FILES:
    file = request.FILES['picture']
    filename = file['filename']
    fd = open('%s/%s' % (MEDIA_ROOT, filename), 'wb')
fd.write(file['content'])
    fd.close() 
    if form.is_valid():
       new_obj = form.save(commit=False)
       new_obj.picture = form.cleaned_data['picture']
       new_obj.created_by = request.user

       new_obj.save()
       return HttpResponseRedirect('.')    
  else:
       form = OpencvForm()     
  return render_to_response('opencv/opencv_form.html', {
       'form': form,
       }, 
      context_instance=RequestContext(request))  

but i don't seem to save the picture in my database... something is wrong, and i can't figure out what :(

5 Answers 5

6

You don't make it easy to help you - you give no description of what actually happens.

However my guess is that you haven't included enctype="multipart/form-data" in your HTML form element.

I would recommend reading the file upload documentation - you're doing a few things manually here that could be handled for you by Django.

1
  • i added enctype="multipart/form-data" in my html form, but then my error is: 'InMemoryUploadedFile' object is unsubscriptable
    – dana
    May 21, 2010 at 22:02
2

try: filename = file.name!

http://docs.djangoproject.com/en/dev/topics/http/file-uploads/

0
1

I've found the solution:

def save_opencv(request):
    if request.method == 'POST':
    form = OpencvForm(request.POST, request.FILES) 
    if form.is_valid():
       handle_uploaded_file(request.FILES['picture'])
       new_obj = form.save(commit=False)
       new_obj.created_by = request.user

       new_obj.save()
       return HttpResponseRedirect('.')    
   else:
       form = OpencvForm()     
  return render_to_response('opencv/opencv_form.html', {
       'form': form,
       }, 
      context_instance=RequestContext(request))  

and the handle_uploaded_file:

  def handle_uploaded_file(f):
     destination = open('root', 'wb+')
      for chunk in f.chunks():
      destination.write(chunk)
    destination.close()

Please review the indentation. Also, in models.py I have:

picture = models.ImageField(help_text=('Upload an image (max %s kilobytes)' %settings.MAX_PHOTO_UPLOAD_SIZE),upload_to='root',blank=True)

Please be careful that I am saving it to root, which means THE ROOT OF MY SITE , not anything related to the computer file organisation. Also, there is not big deal about the code, the deal is the path - I really didn't understand how to set it at first. Anyway, hope it helps you.

0
0

Your error is here:

filename = file['filename']

It should be:

filename = file.name
1
  • you are right, i was wrong there, but my error persists with : 'InMemoryUploadedFile' object is unsubscriptable
    – dana
    May 22, 2010 at 10:44
0

Change this:

fd.write(file['content']) 

to

fd.write(file.read())

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.