Login

MultiFileWidget

Author:
Digitalxero
Posted:
February 4, 2008
Language:
Python
Version:
.96
Score:
8 (after 8 ratings)

This is a multi file upload widget. That does not require adding multiple file input fields. It requires jQuery.MultiUpload (http://www.fyneworks.com/jquery/multiple-file-upload/)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
from django.newforms.widgets import Input

class MultiFileInput(Input):
    input_type = 'file'
    needs_multipart_form = True

    def render(self, name, value, attrs=None):
        if attrs is None:
            attrs = {}

        if attrs.has_key('class'):
            attrs['class'] += ' multi'
        else:
            attrs['class'] = 'multi'

        name += '[]'

        return super(MultiFileInput, self).render(name, None, attrs=attrs)

    def value_from_datadict(self, data, files, name):
        "File widgets take data from FILES, not POST"
        return files.get(name, None)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 9 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 10 months, 3 weeks ago

Comments

David (on April 22, 2008):

Nice and useful widget, just what I was going to write myself. You saved me the trouble!

Why do you append "[]" to the end of the name though?

#

chester (on March 1, 2009):

Hi! I'm new to django, can you tell me how to use this snippet? I tried creating new form with MultiFileInput as a field but everytime I get error that no files were uploaded..

#

artur_mwaigaryan (on July 16, 2013):

just use HTML5's 'multiple' form attribute,no need for js:

<form action='#' method='post' enctype='multipart/form-data'> <input name='uploads[]' type=file multiple> <input type='submit'> </form>

That should fix it

#

Please login first before commenting.