Login

Copy media files to central location for easier sharing

Author:
jtiai
Posted:
May 17, 2009
Language:
Python
Version:
1.0
Tags:
media static custom command
Score:
0 (after 0 ratings)

I was in need to have pluggable components that all have more or less some media files. I didn't want to pollute my config with dozens of media paths so I wrote this custom command that copies contents <appdir>/app_media to your MEDIA_ROOT/<appname> path.

In template you will refer your media files like {{MEDIA_URL}}/appname/<path to media>

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from django.core.management.base import BaseCommand
from django.utils.importlib import import_module
from django.conf import settings
import os
import shutil

class Command(BaseCommand):
    def handle(self, *args, **options): 
        basedir = os.path.abspath(os.path.curdir)

        for app in settings.INSTALLED_APPS:
            m = import_module(app)
            media_dir = os.path.join(os.path.dirname(m.__file__), 'app_media')
            app_name = app.split('.')[-1]
            dest_dir = os.path.join(settings.MEDIA_ROOT, app_name)
            if os.path.exists(media_dir):
                print "Copying %s/* to %s/" % (media_dir, dest_dir)
                shutil.copytree(media_dir, dest_dir)

More like this

  1. Serve static media files from app/media subdirectory by adamlofts 6 years, 7 months ago
  2. Use the WSGIAccessScript Directive to secure static files based on the Django session by LuckiDog 3 years, 10 months ago
  3. collectmedia command: Copy or link media files from installed apps by exogen 6 years, 6 months ago
  4. Cacheable resources by jbrisbin 6 years, 7 months ago
  5. Serve static media and indexes from app directories [Python2.5, Development only] by adamlofts 6 years, 7 months ago

Comments

carljm (on May 18, 2009):

Good idea, but would be better handled by symlinking rather than copying. Unless you're on Windows, I guess.

#

Please login first before commenting.