【问题标题】:Django Collectstatic command Changed somethingDjango Collectstatic 命令改变了一些东西
【发布时间】:2012-10-15 02:50:39
【问题描述】:

所以我从来没有部署过 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

base.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"),
]

devl.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" />

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

【问题讨论】:

  • STATIC_ROOT 设置?
  • 是的。 static_root 设置已更新并包含在上面。
  • 更重要的是,有了那个查找器,你有DEFAULT_FILE_STORAGE 设置吗?
  • 看起来默认是django.core.files.storage.FileSystemStorage。加载程序查找storage.base_locationFileSystemStorage 上的默认值为MEDIA_ROOT。您可以尝试将MEDIA_ROOT 指向您的静态文件。
  • 这可能都是一个红鲱鱼,虽然我认为FileSystemFinderAppDirectoriesFinder 应该找到你的静态文件,即使第三个失败。顺便问一下,从您的 PROJECT_ROOT 到您的site_base.css 的路径是什么?

标签: django django-settings django-staticfiles django-static


【解决方案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")

【讨论】:

  • 当您在页面上查看源代码时,{{ STATIC_URL }} 的输出是什么?
  • "" 在源视图中还有“找不到页面”模板作为该行下方的下拉菜单。
  • 这就是问题所在 - STATIC_URL 不在您的上下文中。你能发布查看代码吗?
  • 啊——你注册staticfiles context processor了吗?
  • 好的,所以我用“django.core.context_processors.static”更新了模板上下文处理器,html 视图源现在显示为 发生服务器错误。请联系管理员。
猜你喜欢
  • 2013-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 1970-01-01
  • 2023-03-08
相关资源
最近更新 更多