django.contrib.staticfiles.finders.find 需要帮助

发布于 2024-10-29 18:02:34 字数 1979 浏览 6 评论 0原文

我有以下 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

从 django_compressor 的 fork 中指定的 templatetag 中,我调用一个执行以下操作的类:

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

因此,如果 django 是新版本,它会尝试使用 staticfiles 查找器,否则模仿其基本查找器通过 STATIC_ROOTS 配置变量。

最后,问题是:给定上面的文件夹布局,我将 "folder1/file1" 传递给 finders.find 方法,并且我有以下设置:

INSTALLED_APPS = (

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

    'compressor',
    'app_folder',
)

并且

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

我得到 <来自 find 调用的 code>None。

有什么想法吗?

UPD。更奇怪的细节:我跑了

python manage.py shell

并且做了

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

它给了我正确的结果......

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不念旧人 2024-11-05 18:02:34

答案是:导入整个模块,而不仅仅是 find 方法

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

知你几分 2024-11-05 18:02:34

我在新网站中使用静态文件,需要一段时间才能设置。你的settings.py里有类似的东西吗?

另请查看:
Django 静态文件应用帮助

# 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

I am using the static files with my new site and it took a while to get set up. Do you have something like this in your settings.py?

Also check out:
Django staticfiles app help

# 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文