Friday, September 2, 2011

Re: Problem with file upload!

is there any error messages you seeing? or logged? does the file exists on the filesystem?

On Fri, Sep 2, 2011 at 8:39 PM, Ludvig <ludvigthorsen@gmail.com> wrote:
Hello,

Usually only read these posts so i hope i'm doing this right!

Recently my file upload just stopped working, and i've no idea what
i've changed. I noticed that request.FILES is empty so i've googled
some and cant figured out my problem.

My form looks like this

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

and my code like this,


class PhotoUploadView(FormView):
   template_name ="album/photo_upload.html"
   form_class = PhotoUploadForm
   def get_context_data(self,**kwargs):
       context =
super(PhotoUploadView,self).get_context_data(**kwargs)
       context['user_info'] = self.request.user
       if 'upload_form' in kwargs:
           context['upload_form'] = kwargs['upload_form']
       else:
           context['upload_form'] = PhotoUploadForm()
       album = get_object_or_404(Album,id=self.kwargs['album_id'])
       context['album'] = album
       return context

   def form_valid(self,form):
       print 'kek'
   def post(self,*args,**kwargs):
       print self.request.FILES
       print self.request.raw_post_data
       if self.request.method == "POST":
           form =
PhotoUploadForm(self.request.POST,self.request.FILES)
           if form.is_valid():
               photo = Photo()
               photo.title = form.cleaned_data['title']
               photo.summary = form.cleaned_data['description']
               photo.album = get_object_or_404(Album,id =
kwargs['album_id'])
               photo.is_cover_photo = True
               path =
self.generate_filename(self.request.FILES['photo'].name,self.request.user,kwargs['album_id'])
               destination = open(path,"wb+")
               for chunk in self.request.FILES['photo'].chunks():
                   destination.write(chunk)
               destination.close()
               photo.imagePath = path
               photo.save()
       return
self.render_to_response(self.get_context_data(upload_form=form))

   def generate_filename(self,filename,user,album_id):
       filename = os.path.splitext(filename)[1]
       return settings.MEDIA_ROOT + "/profiles/" + user.username +
"/" + "albums/" + album_id + "/" + str(hashlib.md5(str(time.time() +
user.id)).hexdigest()) + filename


Also,  i'm a inexperienced programmer. Still only a hobby so be kind
to me,

thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment