0

I have given my code below. It is having a image field. I'm using django forms and custom templates to fill and store this information. And I want to allow users to upload 0 to 10 images for each book as per their wish. So how can I achieve it. My requirements are as follows:

  1. Minimum - 0 file, Maximum - 10 files
  2. Allowed file types - *.jpg, *.jpeg, *.gif, *.png
  3. Either it can be a browse file field or a button which will call a window to select multiple files.
  4. I want to save and retrieve these images and edit them in future.

models.py

class Book(models.Model):
    title       = models.CharField(max_length = 200)
    author      = models.CharField(max_length = 200)
    cost        = models.IntegerField()
    description = models.TextField()
    image       = models.ImageField(upload_to = 'images/books/', null = True, blank = True)

forms.py

class BookForm(forms.ModelForm):

    class Meta:
        model = Book
        widgets = {
            'location'   : forms.TextInput(attrs={'placeholder': 'Location'}),
            'title'      : forms.TextInput(attrs={'placeholder': 'Title'}),
            'author'     : forms.TextInput(attrs={'placeholder': 'Author'}),
            'cost'       : forms.TextInput(attrs={'placeholder': 'Price'}),
            'description': forms.Textarea(attrs={'placeholder': 'Description'}),
        }

Before asking this question, I tried Garmoncheg django_multiuploader and Scompt multifile widget. But none of them helped me. I'm working on this task for the past two days, but couldn't get it working. So Please suggest me a way of completing this task successfully.

arulmr
  • 8,620
  • 9
  • 54
  • 69

1 Answers1

1

What I would do is separate the "image" field into a separate model entirely, then use an inline formset factory to upload multiple images for a Book.

Have a look at the formset documentation, and I think you'll catch on pretty quickly. Hope that helps you out.

Brandon Taylor
  • 33,823
  • 15
  • 104
  • 144
  • Hi Brandon. Using an inline formset is an excellent idea, when it comes to django admin. But I want this functionality in django custom templates and I want to display dynamic image upload fields in them. So how can I achieve it. Need a little more explanation on that. Thank you. – arulmr Mar 20 '12 at 09:19
  • Formsets aren't just for admin. You can use them in any view anywhere. There are also jQuery plugins to dynamically add and remove forms from the formset, or you could write your own. In the formsets documentation, have a look at the section on using formsets in views and templates: https://docs.djangoproject.com/en/1.3/topics/forms/formsets/#using-a-formset-in-views-and-templates – Brandon Taylor Mar 20 '12 at 12:55
  • Thank you Brandon. Completed the task with inline formsets and jQuery. Thanks once again. – arulmr Mar 23 '12 at 10:16
  • 1
    hi arulmr.. I am stucked in the same problem as yours.. How you did this can you give me some idea.. I also want a browse button when I click I should add multiple images.. – gamer Mar 18 '15 at 21:42