0

所以我从来没有部署过 Django 应用程序,我正在努力加快速度。我运行了 collectstatic 命令,现在我的静态文件都不会呈现。当我运行 findstatic 命令时,我收到一个异常,上面写着:

django.core.exceptions.ImproperlyConfigured: The storage backend of the staticfiles finder     <class 'django.contrib.staticfiles.finders.DefaultStorageFinder'> doesn't have a valid location.

我的模板呈现只是 find 但我似乎无法弄清楚为什么找不到 css 文件。从我的设置模块中突出显示:

settings/
    base.py
    devel.py
    prod.py

基础.py

cwd = os.path.dirname(os.path.abspath(__file__)) 
PROJECT_ROOT = cwd[:-9] # chop off "settings/"

STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.DefaultStorageFinder',
]

TEMPLATE_LOADERS = [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.template.loaders.eggs.Loader',
]

TEMPLATE_DIRS = [
os.path.join(PROJECT_ROOT, "templates"),
]

开发者.py

STATIC_URL = "/site_media/static/"

STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, "site_media", "static"),
]

STATIC_ROOT = os.path.join(PROJECT_ROOT, "site_media", "static")

site_base.html

<link rel="stylesheet" href="{{ STATIC_URL }}css/site_base.css" />

感谢您的帮助,因为我很难过。

4

1 回答 1

1

更新:

原来是缺少上下文处理器。要在模板中获取 STATIC_URL 设置,您必须注册staticfiles上下文处理器:

TEMPLATE_CONTEXT_PROCESSORS = [
    ...
    'django.core.context_processors.static',
    ...
]

第一刺:

看起来您需要将该目录添加到静态源列表中(回复:上面的评论):

# list of input paths for collectstatic
STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, "tulsa", "static"),

    # you'll want to remove this path:
    #os.path.join(PROJECT_ROOT, "site_media", "static"),
]

# output path for collectstatic
STATIC_ROOT = os.path.join(PROJECT_ROOT, "site_media", "static")
于 2012-10-26T00:06:31.353 回答