Answer the question
In order to leave comments, you need to log in
How to generate the path and name of the uploaded file to the server (django)?
The user sends an image to the server, I need to generate the path and file name, how to do this?
Answer the question
In order to leave comments, you need to log in
Something similar to this
def get_file_path(instance, filename):
ext = filename.split('.')[-1]
filename = "%s.%s" % (str(instance.uuid), ext)
return os.path.join('uploads','data', filename)
class Image(models.Model):
name = models.CharField(blank=True, max_length=250)
image = models.FileField(upload_to=get_file_path)
uuid = UUIDField(auto=True)
class File(models.Model):
def make_upload_path(instance, filename):
name, extension = os.path.splitext(filename)
filename = '%s%s' % (slugify(name), extension)
d = datetime.datetime.now()
return u'upload/%s/%s/%s/%s' % (instance._meta.app_label,
instance._meta.module_name,
d.strftime('%d_%m_%Y'),
filename.lower())
file = SorlImageField(upload_to=make_upload_path, verbose_name='иконка',
blank=True)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question