I am using django-sendfile
to protect certain files.
The problem is that when the file is displayed on the frontend it uses the MEDIA_ROOT
location which is not where the file actually is.
The actual link (that checks permissions) that I want to use is:
http://127.0.0.1:8000/leave/supporting_doc/57
yet the link displayed is:
http://127.0.0.1:8000/media/leave/user_2/2018-09-06-logo.png
The field in the model is:
supporting_doc = models.FileField(
upload_to=user_leave_directory_path,
storage=SENDFILE_STORAGE,
blank=True,
validators=[FileExtensionValidator(ACCEPTED_FILE_TYPES)]
)
The upload to method give a link relative to MEDIA_ROOT
:
def user_leave_directory_path(instance, filename):
'''Get the user directory for leave supporting document'''
# file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
return 'leave/user_'\
f'{instance.user.id}/{instance.start_date}-{filename}'