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
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Good idea, but would be better handled by symlinking rather than copying. Unless you're on Windows, I guess.
#
Please login first before commenting.