0

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.

Admin leave file link

Frontend leave file link

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}'
tread
  • 10,133
  • 17
  • 95
  • 170
  • You need to fix your `MEDIA_URL` setting if the URL being generated is incorrect. Looks like `MEDIA_URL` needs to be set to `/media/`. – solarissmoke Mar 22 '18 at 13:15
  • The question is a bit more complex because I am uploading to protected locations using `django-sendfile`, The outputted url is correct for non-protected media. – tread Mar 22 '18 at 13:22
  • This thread might have a hint - maybe you need to configure or override SENDFILE_STORAGE to not use MEDIA_ROOT: https://stackoverflow.com/questions/1729051/django-upload-to-outside-of-media-root – whp Mar 22 '18 at 15:29
  • Yes that works to give the actual protected location on the server. I'm just trying to override the whole thing with a custom url. – tread Mar 27 '18 at 11:34

0 Answers0