Answer the question
In order to leave comments, you need to log in
How to store user files in Django?
Please share your thoughts or experience in implementing file storage for different users. The task is this: it is necessary that users in their personal accounts be able to upload files through the form, and that these files are available only to their creators.
Something like this: the user user1
uploads his files to /storage/path/to/user1/date/time/file.name
, and in his personal account he has a link to this file. Also, the user can delete this file, and it should be deleted from the repository too.
Suddenly someone implemented something similar, or there are ready-made modules for django. I just can't figure out what query to ask this on Google.
Answer the question
In order to leave comments, you need to log in
Create a model (let's call it UserFiles) with fields file and a key for the user model.
Take request.user.userfiles_set.all() in the view or filter as you need.
For more complex implementations read more documentation.
in settings.py add an indication of the location of the media
MEDIA_URL = '/media/' #например
MEDIA_ROOT = '/srv/files/media' #например
class ModelClass(models.Model):
<поле> = models.ImageField(upload_to=rename_image, blank=True, verbose_name='...')
def rename_image(instance, filename):
image_name = md5(str(time.time()).encode()).hexdigest()
image_type = filename.split('.')[-1]
return 'imgs/{}.{}'.format(image_name, image_type)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question