Uploading multiple files in a ModelForm

cyberjoac

I have a simple model in Django:

class Test(models.Model):
    user = models.ForeignKey(User, null=True, blank=True)
    title = models.CharField(max_length=180, unique=True)
    file1 = models.FileField(upload_to=download_loc)
    preview = models.FileField(upload_to=preview_loc)

file1 is a file, and I generate dynamically on the client-side a "preview" of file1 (jpg image).

I am trying to upload the two of them on the same form, but I cannot find a simple way to do this. How could I do that with Jquery?

This is the class for the form I have:

class TestForm(ModelForm):
    class Meta:
        model = Test
        fields = ('title', 'file1') # I don't want to put preview here because it should be done without the user intervention (I don't want the user to see a button with choose file for the preview)

Thanks a lot!

Artur Barseghyan

If you really want to intergrate thumbnail generation to the model level, use the django-imagekit https://pypi.python.org/pypi/django-imagekit

Example:

from django.db import models
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFill

class Profile(models.Model):
    avatar = models.ImageField(upload_to='avatars')
    avatar_thumbnail = ImageSpecField(source='avatar',
                                      processors=[ResizeToFill(100, 50)],
                                      format='JPEG',
                                      options={'quality': 60})

profile = Profile.objects.all()[0]
print profile.avatar_thumbnail.url    # > /media/CACHE/images/982d5af84cddddfd0fbf70892b4431e4.jpg
print profile.avatar_thumbnail.width  # > 100

Although, in most of the cases you shouldn't be adding the preview to the model, since there are a lot of packages for generating thumbnails on the fly (on the template level) in Django:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJs Uploading multiple files .then

From Dev

Multiple Files Uploading in Laravel

From Dev

Issues with Uploading Multiple files with PHP

From Dev

Uploading multiple files in the same request

From Dev

Multiple files uploading in multidimensional array

From Dev

PHP code for uploading multiple files

From Dev

Multiple files uploading not working in Codeigniter

From Dev

Uploading multiple files using Javascript

From Dev

Uploading multiple files and renaming - PHP

From Dev

Multiple files not uploading in play framework

From Dev

Multiple files uploading not working in Codeigniter

From Dev

Uploading multiple files in Laravel 5

From Dev

Uploading multiple files in single multipart post

From Dev

FineUploader uploading multiple files as one request

From Dev

Uploading then renaming multiple files over ssh

From Dev

Selecting multiple files and uploading them using Jersey

From Dev

Issue uploading multiple files of the same name

From Dev

Uploading multiple files with multer, but from different fields?

From Dev

uploading multiple files and zip them in a folder in the server

From Dev

Uploading and processing multiple files using MVC

From Dev

Uploading multiple files via FTP using curl

From Dev

Uploading multiple files in single multipart post

From Dev

Uploading then renaming multiple files over ssh

From Dev

PHP Uploading Multiple Files With Captions To MySQL

From Dev

Uploading multiple files - Getting different values

From Dev

Uploading multiple files with one request in Podio (suggestion)

From Dev

Upload multiple files to server with php not uploading to server all files

From Dev

Upload multiple files to server with php not uploading to server all files

From Dev

Mysql updating multiple textboxes and uploading multiple files at the same time

Related Related

  1. 1

    AngularJs Uploading multiple files .then

  2. 2

    Multiple Files Uploading in Laravel

  3. 3

    Issues with Uploading Multiple files with PHP

  4. 4

    Uploading multiple files in the same request

  5. 5

    Multiple files uploading in multidimensional array

  6. 6

    PHP code for uploading multiple files

  7. 7

    Multiple files uploading not working in Codeigniter

  8. 8

    Uploading multiple files using Javascript

  9. 9

    Uploading multiple files and renaming - PHP

  10. 10

    Multiple files not uploading in play framework

  11. 11

    Multiple files uploading not working in Codeigniter

  12. 12

    Uploading multiple files in Laravel 5

  13. 13

    Uploading multiple files in single multipart post

  14. 14

    FineUploader uploading multiple files as one request

  15. 15

    Uploading then renaming multiple files over ssh

  16. 16

    Selecting multiple files and uploading them using Jersey

  17. 17

    Issue uploading multiple files of the same name

  18. 18

    Uploading multiple files with multer, but from different fields?

  19. 19

    uploading multiple files and zip them in a folder in the server

  20. 20

    Uploading and processing multiple files using MVC

  21. 21

    Uploading multiple files via FTP using curl

  22. 22

    Uploading multiple files in single multipart post

  23. 23

    Uploading then renaming multiple files over ssh

  24. 24

    PHP Uploading Multiple Files With Captions To MySQL

  25. 25

    Uploading multiple files - Getting different values

  26. 26

    Uploading multiple files with one request in Podio (suggestion)

  27. 27

    Upload multiple files to server with php not uploading to server all files

  28. 28

    Upload multiple files to server with php not uploading to server all files

  29. 29

    Mysql updating multiple textboxes and uploading multiple files at the same time

HotTag

Archive