A
A
archi19982014-10-18 13:36:46
Django
archi1998, 2014-10-18 13:36:46

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

2 answer(s)
A
Alexander, 2014-10-18
@ROR191505

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)

V
Vic, 2014-10-20
@korsvs

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 question

Ask a Question

731 491 924 answers to any question