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!
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.
Comments