Uploading multiple images with Django

bencunningham

I am working on a Django app and am having some trouble with the models/forms/templates required to upload images. I have a post and I want there to be a One-to-Many relationship with posts and images. i.e. A post can have any number of images. My first question is - what would the model look like? I am guessing something like:

class Image(models.Model):
    img = models.ImageField(upload_to="photos/",null=True, blank=True)
    posting = models.ForeignKey(Posting, related_name="images")

class Posting(models.Model):
    ...

Is this correct? And my other question is how should I upload multiple images? And I can't figure out what the form should look like. If I want to set a max number of images, could I just go like:

class ImageForm(forms.ModelForm):
    img1 = forms.ImageField()
    img2 = forms.ImageField()

    class Meta:
        model = Image
        fields = ('img1','img2',)

But then I have no idea how to look that up to the view correctly. I am super lost, any help would be greatly appreciated!

catavaran

For this task you should use the inline formsets. Read this docs first for basic understanding of the formsets.

The models in your question are almost valid. Remove the null=True, blank=True arguments from the img field - there is no sense in the Image instances without image itself.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django multiple images not uploading

From Dev

Django REST: Uploading and serializing multiple images

From Dev

Uploading multiple images with volley?

From Dev

Uploading multiple images in codeigniter?

From Dev

Uploading multiple images with paperclip?

From Dev

Uploading multiple images in codeigniter?

From Dev

DRF: Uploading images in Django

From Dev

Uploading Multiple Images using CarrierWave

From Dev

Multiple images rename & uploading in PHP

From Dev

Multiple images rename & uploading in PHP

From Dev

Django-Filebrowser and uploading images

From Dev

All images not uploading in multiple images upload

From Dev

Uploading multiple images from Share Extension

From Dev

Uploading multiple images with other parameters in Swift

From Dev

Uploading multiple images to mysql database on Apache server

From Dev

PHP: Uploading multiple images to imgur at once

From Dev

multiple images uploading and converting them to thumbnails in cakephp

From Dev

PHP: Uploading multiple images at the same time

From Dev

Uploading multiple images in webservice using PHP

From Dev

Uploading multiple images using SRWebClient in Swift

From Dev

uploading/downloading multiple images the right way?

From Dev

Rails and carrierwave uploading multiple images fails

From Dev

filereader result order changing while uploading multiple images in javascript

From Dev

Issue uploading multiple images to a page with "upload" button...

From Dev

Uploading multiple images one to each row PHP and Mysqli db

From Dev

Jquery : Post Request is breaking while uploading multiple images

From Dev

Uploading multiple images on twitter using Ruby on Rails Twitter Gem

From Dev

Rails, uploading multiple images with carrierwave, but json not supported by my database

From Dev

only one record is saving when uploading multiple images in yii

Related Related

  1. 1

    Django multiple images not uploading

  2. 2

    Django REST: Uploading and serializing multiple images

  3. 3

    Uploading multiple images with volley?

  4. 4

    Uploading multiple images in codeigniter?

  5. 5

    Uploading multiple images with paperclip?

  6. 6

    Uploading multiple images in codeigniter?

  7. 7

    DRF: Uploading images in Django

  8. 8

    Uploading Multiple Images using CarrierWave

  9. 9

    Multiple images rename & uploading in PHP

  10. 10

    Multiple images rename & uploading in PHP

  11. 11

    Django-Filebrowser and uploading images

  12. 12

    All images not uploading in multiple images upload

  13. 13

    Uploading multiple images from Share Extension

  14. 14

    Uploading multiple images with other parameters in Swift

  15. 15

    Uploading multiple images to mysql database on Apache server

  16. 16

    PHP: Uploading multiple images to imgur at once

  17. 17

    multiple images uploading and converting them to thumbnails in cakephp

  18. 18

    PHP: Uploading multiple images at the same time

  19. 19

    Uploading multiple images in webservice using PHP

  20. 20

    Uploading multiple images using SRWebClient in Swift

  21. 21

    uploading/downloading multiple images the right way?

  22. 22

    Rails and carrierwave uploading multiple images fails

  23. 23

    filereader result order changing while uploading multiple images in javascript

  24. 24

    Issue uploading multiple images to a page with "upload" button...

  25. 25

    Uploading multiple images one to each row PHP and Mysqli db

  26. 26

    Jquery : Post Request is breaking while uploading multiple images

  27. 27

    Uploading multiple images on twitter using Ruby on Rails Twitter Gem

  28. 28

    Rails, uploading multiple images with carrierwave, but json not supported by my database

  29. 29

    only one record is saving when uploading multiple images in yii

HotTag

Archive