Search code examples
pythondjangostatic-files

django.contrib.staticfiles.finders.find help needed


I have the following Django project layout:

project_folder/
    app_folder/
        static/
           folder1/
               file1
        templates/
        ...
    compressor -> (symlink to django_compressor app with my modifications)
    __init__.py
    manage.py
    settings.py
    urls.py

From the templatetag specificed in my fork of django_compressor I'm calling a class that does the following:

class StorageMixin(object):
    from django import VERSION as DJANGO_VERSION
    if DJANGO_VERSION[:2] >= (1, 3):
        from django.contrib.staticfiles.finders import find as _django_find
        def _find_file_path(self, path):
            return self._django_find(path)
    else:
        def _find_file_path(self, path):
            static_roots = getattr(settings, 'STATIC_ROOTS', []) + [settings.COMPRESS_ROOT]
            for root in static_roots:
                filename = os.path.join(root, basename)
                if os.path.exists(filename):
                    return filename
            return None

So, if django is of the new version, it tries to use staticfiles finders, otherwise mimics its basic finder through the STATIC_ROOTS config variable.

Finally, the question: given the folders layout above, I pass "folder1/file1" to finders.find method, and I have the following settings:

INSTALLED_APPS = (

    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'compressor',
    'app_folder',
)

and

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

and I get None from the find call.

Any ideas?

UPD. More strange details: I ran

python manage.py shell

and did

from django.contrib.staticfiles.finders import find
find("folder1/file1")

And it gave me the correct result...


Solution

  • The answer was: import the whole module, not only the find method