Reputation: 665
I have a Django app that displays surveillance video thumbnails (as hyperlinks to their respective videos) created by another surveillance app in a way that works better for me than the surveillance app displays them natively.
Basically I have a script that moves the files every half hour from the surveillance app storage location to the location that production server will serve them to the Django app.
Say these files are moved to this location on the server:
/media/djangoAppMedia/
For development thus far, I've had my MEDIA_ROOT hard-coded to this path:
MEDIA_ROOT = '/media/djangoAppMedia/'
I'd like to change this to a relative path going forward, but all examples that I can find always reference a media root that is within the main Django app project.
Ex: MEDIA_ROOT = os.path.join(ENV_PATH, 'media/')
.
How can I set my media root to a relative path that's not within my Django directory?
Or, alternatively, should I instead move the files into a media directory within my project? Or, even use static for these?
There's some grey area to me since these files aren't "user submitted" like it seems is the usual use-case for the media root, but they also don't seem to fit into the normal django static use-case.
Upvotes: 1
Views: 4179
Reputation: 27311
The question then becomes, relative to what? The production webserver's opinion of what the current directory is may be surprising.
If you meant relative to where your source file is, then:
DIRNAME = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(DIRNAME, 'some', 'path', 'under', 'this', 'folder')
If you mean a path completely independent of your project, then you'll need some way to find it on both dev and prod. Environment variables are one way to do this:
MEDIA_ROOT = os.environ['MY_VIDEO_FILES']
you'll have to make sure that environment variable is set by whatever starts your runtime environment.
You can of course use an if-statement in your settings.py:
if DEBUG:
MEDIA_ROOT = os.path.abspath('../../some/relative/path')
else:
MEDIA_ROOT = '/some/absolute/path/on/prod/server'
this works fine, and is easiest, especially if you're the only developer.
Update: Apache, etc.
With the mod_env
Apache module enabled, it is possible to define environment variables (in your virtualhost):
SetEnv MEDIA_ROOT /absolute/path/to/media/root
those are apache env vars, so you'll have to change your wsgi.py
file to get them to Django (adapted from http://ericplumb.com/blog/passing-apache-environment-variables-to-django-via-mod_wsgi.html):
from django.core.wsgi import get_wsgi_application
_application = get_wsgi_application()
def application(environ, start_response):
os.environ['MEDIA_ROOT'] = environ.get('MEDIA_ROOT', '')
return _application(environ, start_response)
That's a lot of work, but if you're setting up your own server then it might be worthwile.
It's easier to just use MEDIA_ROOT = '/media/'
and let Apache redirect it for you (in your virtualhost):
Alias /media/ /absolute/path/to/media/root
In your settings you could then do:
MEDIA_ROOT = os.environ.get("MEDIA_ROOT", '/media/')
and define the environment variable in your development environment..
ps: depending on what software you're writing (e.g. reusable packages) you might not need to include a settings.py
file..
Upvotes: 3