python - Cannot identify image file ImageFieldFile -
i'm using django python, in models.py declared tournament model , attribute image, save method:
import datetime django.db import models django.utils import timezone tinymce.models import htmlfield rules.models import rule news.models import tag, news courts.models import court news.models import replace_spaces datetime import datetime sorl.thumbnail import imagefield, get_thumbnail pil import image django.core.files.storage import default_storage storage django.conf import settings class tournament(models.model): name = models.charfield(max_length=50, default='', verbose_name='nombre') name_url = models.charfield(max_length=200, default='', editable=false) inscription_opening_date = models.datefield('fecha de apertura de inscripcion') inscription_closing_date = models.datefield('fecha de cierre de inscripcion') tournament_starting_date = models.datefield('fecha de inicio del torneo') tournament_ending_date = models.datefield('fecha de finalizacion del torneo') description = htmlfield(default='', verbose_name='descripcion del torneo', blank=true) image = imagefield(upload_to='tournamentpic', verbose_name='imagen') situation = models.foreignkey(tournamentsituation, default='', verbose_name='estado del torneo') tournament_format = models.foreignkey(tournamentformat, default='', verbose_name='formato del torneo') regulation = models.foreignkey(rule, default='', verbose_name='reglamento') tournament_type = models.foreignkey(tournamenttype, default='', verbose_name='tipo de torneo') tournament_category = models.foreignkey(tournamentcategory, default='', verbose_name='categoria de torneo') quota = models.integerfield(default=0, verbose_name='cupos') tag = models.manytomanyfield(tag) price = models.decimalfield(default=0, max_digits=6, decimal_places=2, blank=true) zones = models.integerfield(default=0, verbose_name='zonas', blank=true) knockout_opening_choices = ( ('64', 'ronda de 128'), ('32', 'ronda de 64'), ('16', 'ronda de 32'), ('8', 'octavos de final'), ('4', 'cuartos de final'), ('2', 'semifinal'), ) knockout_opening_round = models.charfield(max_length=2, verbose_name='instancia de inicio de llaves', choices=knockout_opening_choices, default='32', blank=true) singles = models.integerfield(default=0, verbose_name='numero de singles', blank=true) doubles = models.integerfield(default=0, verbose_name='numero de doubles', blank=true) photo_full_size = models.charfield(max_length=255, blank=true, editable=false) def save(self): self.name_url = replace_spaces(self.name) sizes = {'full': {'height': 415, 'width': 620},} extension = self.image.name.rsplit('.', 1)[1] # file extension = datetime.now() date = now.strftime("%h-%m-%s-%d-%m-%y") self.image.name = date +'.png' filename = date super(tournament, self).save() default_format = 'png' im = image.open(self.image) if extension not in ['jpg', 'jpeg', 'gif', 'png']: sys.exit() new_size = im.resize((sizes['full']['width'], sizes['full']['height']), image.antialias) medname = "-" + str(sizes['full']['width']) + "x" + str(sizes['full']['height']) + ".png" thumb_path = upload_to + filename + medname fh = storage.open(thumb_path, "w") new_size.save(fh,default_format) fh.close() self.photo_full_size = thumb_path
when save new tournament in admin, following error:
cannot identify image file <imagefieldfile: 16-16-58-19-06-2015.png>
im uploading pictures , media files s3. strange thing in model, exact same configuration works perfectly! here other model:
from django.db import models tinymce.models import htmlfield datetime import datetime sorl.thumbnail import imagefield, get_thumbnail pil import image django.core.files.storage import default_storage storage django.conf import settings import sys media_url = getattr(settings, 'media_url') upload_to = 'aboutpic/' class name(models.model): name = models.charfield(max_length=20, default='', verbose_name='nombre') lastname = models.charfield(max_length=20, default='', verbose_name='apellido') def __str__(self): return self.name + ' ' + self.lastname class about(models.model): image = imagefield(upload_to='aboutpic', verbose_name='imagen de portada') introduction = htmlfield(default='', verbose_name='cuerpo de noticia', blank=true) creation_date = models.datefield('fecha de publicacion', default=datetime.now(), editable=false) names = models.manytomanyfield(name) photo_full_size = models.charfield(max_length=255, blank=true, editable=false) def save(self): sizes = {'full': {'height': 415, 'width': 620},} extension = self.image.name.rsplit('.', 1)[1] # file extension = datetime.now() date = now.strftime("%h-%m-%s-%d-%m-%y") self.image.name = date +'.png' filename = date super(about, self).save() print self.image default_format = 'png' im = image.open(self.image) if extension not in ['jpg', 'jpeg', 'gif', 'png']: sys.exit() new_size = im.resize((sizes['full']['width'], sizes['full']['height']), image.antialias) medname = "-" + str(sizes['full']['width']) + "x" + str(sizes['full']['height']) + ".png" thumb_path = upload_to + filename + medname fh = storage.open(thumb_path, "w") new_size.save(fh,default_format) fh.close() self.photo_full_size = thumb_path super(about, self).save() def get_full_size(self): return media_url + self.photo_full_size
Comments
Post a Comment