Pregunta

Tengo el siguiente diseño de proyecto Django:

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

Desde la etiqueta de plantilla especificada en mi bifurcación de django_compressor, llamo a una clase que hace lo siguiente:

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

Entonces, si Django es de la nueva versión, intenta usar buscadores de archivos estáticos; de lo contrario, imita su buscador básico a través de la variable de configuración STATIC_ROOTS.

Finalmente, la pregunta:dado el diseño de carpetas anterior, paso "folder1/file1" a finders.find método, y tengo la siguiente configuración:

INSTALLED_APPS = (

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

    'compressor',
    'app_folder',
)

y

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

y lo entiendo None desde el find llamar.

¿Algunas ideas?

UPD.Más detalles extraños:yo corrí

python manage.py shell

y lo hizo

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

Y me dio el resultado correcto...

¿Fue útil?

Solución

La respuesta fue:importar el módulo completo, no sólo el find método

Otros consejos

Estoy usando los archivos estáticos con mi nuevo sitio y me tomó un tiempo configurarlo.¿Tienes algo como esto en tu settings.py?

Consulte también:Ayuda de la aplicación Django staticfiles

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/static/"
STATICFILES_ROOT =  '/path/to/my/site/static/'
STATIC_ROOT = STATICFILES_ROOT

# URL that handles the static files served from STATICFILES_ROOT.
# Example: "http://static.lawrence.com/", "http://example.com/static/"
STATICFILES_URL = '/static/'
STATIC_URL = STATICFILES_URL

# URL prefix for admin media -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
ADMIN_MEDIA_PREFIX = '/static/admin/'

# A list of locations of additional static files
STATICFILES_DIRS = (
    ("game",      BASE_DIR + "/game/media"),
    ("sitemedia", BASE_DIR + "/templates/media/"),
)
STATIC_DIRS = STATICFILES_DIRS

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
STATIC_FINDERS= STATICFILES_FINDERS
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top