python - django mutiupload not working -
what im trying upload multiply files @ once code pretty old , wondering if 1 can or there better way on doing
here code model.py
class document(models.model): docfile = models.filefield(upload_to=only_filename) def __unicode__(self): return self.docfile.name @models.permalink def get_absolute_url(self): return ('upload-delete', )
forms.py
class myuploadform(forms.form): docfile = multifilefield(max_num=3, min_num=1, max_file_size=1024*1024*5) def save(self, commit=true): super(myuploadform, self).save(commit=commit) each in self.cleaned_data['docfile']: att = attachment(parent=self.instance, file=each) att.save() return self.instance
and view.py
@login_required def list(request): if request.method == 'post': form = myuploadform(request.post, request.files) if form.is_valid(): newdoc = document(docfile = request.files['docfile']) newdoc.user = request.user newdoc.save() return httpresponseredirect(reverse('multiupload.views.list')) else: form = myuploadform() # empty, unbound form documents = document.objects.all return render_to_response( 'sample/index.html', {'documents': documents, 'form': form}, context_instance=requestcontext(request) )
maybe it's not correct way...
don't override save in forms.py , put create each new object in views after valid() like...
def list(request): if request.method == 'post': form = myuploadform(request.post, request.files) if form.is_valid(): newdoc = form.cleaned_data['docfile'] file_obj in request.files.getlist('docfile'): document.objects.create(docfile=file_obj) return httpresponseredirect(reverse('multiupload.views.list'))
Comments
Post a Comment