validators=[validate_file_extension], default='StandartAvatar.png')
как я могу сделать так, чтобы оно сохранялу картунку с тем именем, что мне надо?
def validate_file_extension(value):
import os
from django.core.exceptions import ValidationError
ext = os.path.splitext(value.name)[1]
valid_extensions = ['.jpg', '.png', '.bmp', '.tiff', '.jpeg', '.gif']
if not ext.lower() in valid_extensions:
raise ValidationError(u'Unsupported file extension.')
10 мин
def upload_to(instance, filename): """ Returns path to upload file as @string @string: app_name/model_name/4_uuid4_symbols/uuid4.ext """ ext = filename.split('.')[-1] filename = '%s.%s' % (uuid.uuid4(), ext) basedir = os.path.join(instance._meta.app_label, instance.__class__.__name__.lower()) return os.path.join(basedir, filename[:4], filename) __________________________________________________________________ profile_avatar = models.ImageField( upload_to=upload_to, validators=[validate_file_extension] )
Обсуждают сегодня