Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

371
Vistas
Django: relative path for media_root not located within project directory

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.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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..

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda