【问题标题】:django.contrib.staticfiles.finders.find help neededdjango.contrib.staticfiles.finders.find 需要帮助
【发布时间】:2011-03-31 10:20:53
【问题描述】:

我有以下 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 分支中指定的模板标签,我正在调用一个执行以下操作的类:

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 是新版本,它会尝试使用静态文件查找器,否则会通过 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 电话中得到None

有什么想法吗?

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

python manage.py shell

做了

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

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

【问题讨论】:

    标签: python django static-files


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我在我的新站点中使用静态文件,设置需要一段时间。你的settings.py中有这样的东西吗?

      还可以查看: 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
      

      【讨论】:

      • 是的,我拥有这些设置的大部分。我认为不需要重复的设置(就像你有 STATIC_ROOT = STATICFILES_ROOT)。我也不使用 STATIC_DIRS,因为我希望 'django.contrib.staticfiles.finders.AppDirectoriesFinder' 在这种特殊情况下工作
      • 我已经阅读了那个问题,它就是答案——虽然他们没有回答我的问题——我认为一切都已根据需要进行配置,但 find 方法看不到文件。
      猜你喜欢
      • 2022-01-04
      • 2017-10-05
      • 2017-03-23
      • 2012-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多