As usual, despite working on this for a while, the solution hits me after I 
post here.

I have to define a custom finder that uses my custom storage. Bingo!

class CatalogFinder(AppDirectoriesFinder):

    storage_class = AppCatalogStorage

    def __init__(self, apps=None, *args, **kwargs):
        super(CatalogFinder, self).__init__(*args, **kwargs)


STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
    "myapp.finders.CatalogFinder"
)





On Wednesday, 1 November 2017 14:35:08 UTC-4, Stodge wrote:
>
> I'm trying to add an extra directory for each app that will contain 
> Javascript/CSS files. I created a custom storage.py in my app containing a 
> copy of the code from Django's AppDirectoriesFinder class, but I changed 
> source_dir from "static" to "catalog". I'll include the Django code with 
> the modified source_dir so no-one has to check the source code:
>
> class AppCatalogStorage(FileSystemStorage):
>     """
>     A file system storage backend that takes an app module and works
>     for the ``catalog`` directory of it.
>     """
>     prefix = None
>     source_dir = 'catalog'  <------- changed, was 'static'
>
>
>     def __init__(self, app, *args, **kwargs):
>         """
>         Returns a static file storage if available in the given app.
>         """
>         # app is the actual app module
>         mod = import_module(app)
>         mod_path = os.path.dirname(upath(mod.__file__))
>         location = os.path.join(mod_path, self.source_dir)
>         super(AppCatalogStorage, self).__init__(location, *args, **kwargs)
>
>
>
> I added this to my settings file:
>
>     STATICFILES_STORAGE = (
>
>         'myapp.storage.AppCatalogStorage',
>         'django.contrib.staticfiles.storage.StaticFilesStorage'
>     )
>
>
> My understanding here is that I can then do:
>
> (Assuming my app name is fred)
>
> fred/catalog/js/someJavascript.js
> fred/catalog/css/someStylesheet.css
>
>
> I also assumed they would then be accessible as:
>
> /static/fred/js/someJavascript.js
> /static/fred/css/someStylesheet.css
>
>
> However, neither file is available at the expected URL. Did I 
> misunderstand something?
>
> Thanks
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a690f310-567f-451c-be95-88f7ac83a5fb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to