Friday, November 3, 2017

Re: Unable to use custom StaticFilesStorage



On Wed, Nov 1, 2017 at 11:35 AM, Stodge <stodge@gmail.com> 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?


Quite honestly, instead of doing all of this work, why not just amend STATICFILES_DIRS with the location of your extra folder? Or just use the designated 'static/app_name' location for your JS files? Seems like a ton of trouble now and in the future to try and implement your own file structure. A symlink from catalog -> static/fred would also have the same effect if you are on a Linux system. 

-James
 


--
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+unsubscribe@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/CA%2Be%2BciU9xVu9fbEaKz%3DwZ_RA3n1ndK3LmsqdxeZBnWCST4jbqg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment