Package: django-protected-media
Django Protected MediaDjango Protected Media is a Django app that manages media that are considered
sensitive in a protected fashion.Not only does the media get stored in a separate filesystem location, but authorisation
is also required to access it.The application allows for setups where Django performs the authorisation, but
hands off the serving of the file to a web server, like Nginx.Quick startAdd "protected_media" to your INSTALLED_APPS setting like this:INSTALLED_APPS=[...'protected_media.apps.ProtectedMediaConfig',]Include the URLconf in your project urls.py like this::path('protected/', include('protected_media.urls')),Add the following settings tosettings.pyif the defaults need to be tweaked:PROTECTED_MEDIA_ROOT="%s/protected/"%BASE_DIRPROTECTED_MEDIA_URL="/protected"PROTECTED_MEDIA_SERVER="nginx"# Defaults to "django"PROTECTED_MEDIA_LOCATION_PREFIX="/internal"# Prefix used in nginx configPROTECTED_MEDIA_AS_DOWNLOADS=False# Controls inclusion of a Content-Disposition headerUse the new field classes in your models:fromprotected_media.modelsimportProtectedImageField,ProtectedFileFielddefSomeModel(models.Model):document=ProtectedFileField(upload_to="uploads/")picture=ProtectedImageField(upload_to="uploads/")# Files will be stored under PROTECTED_MEDIA_ROOT + upload_toOverviewDjango manages media based on the following definitions:BASE_DIR="/some/application/dir/"MEDIA_ROOT="%s/media/"%BASE_DIRMEDIA_URL="/media/"File- and image fields are typically defined as:document=models.FileField(upload_to="uploads/")picture=models.ImageField(upload_to="uploads/")# Files will be stored under MEDIA_ROOT + upload_toIn a typical production environment one would letnginx(or some other server) serve the media:# Publicly accessible media
location ^~ /media/ {
    alias /some/application/dir/media
}This works well when the media should be publically accessible. However, if the media should be protected, we need a way for Django to check whether the request for the media should only be allowed for logged in (or more stringent criteria) users.Theprotected_mediaapplicationTheprotected_mediaapplication consists ofnewsettings.pyattributes,a customized FileSystemStorage class,a custom handler for the protected media URL andadditional web server configuration if serving vianginxor something similar.Protected media is stored in a different physical location to publically accessible media. The following settings can be specified insettings.py:PROTECTED_MEDIA_ROOT="/some/application/dir/protected/"PROTECTED_MEDIA_URL="/protected"PROTECTED_MEDIA_SERVER="nginx"# Defaults to "django"PROTECTED_MEDIA_LOCATION_PREFIX="/internal"# Prefix used in nginx configWhen defining a file or image field that needs to be protected, we use one of the
classes provided by theprotected_mediaapplication:ProtectedFileFieldProtectedImageFieldProtected file- and image fields are typically defined as:document=ProtectedFileField(upload_to="uploads/")picture=ProtectedImageField(upload_to="uploads/")# Files will be stored under PROTECTED_MEDIA_ROOT + upload_toThese classes have a custom storage backendProtectedFileSystemStoragewhich mananges the filesystem location and URLs associated with protected media.Whennginxis used, the configuration must be updated to look like this:# Publicly accessible media
location /media  {
    alias /some/application/dir/media;
}

# Protected media
location /internal  {
    internal;
    alias /some/application/dir/protected;
}