配置不当的静态文件

Posted

技术标签:

【中文标题】配置不当的静态文件【英文标题】:ImproperlyConfigured staticfiles 【发布时间】:2015-12-23 17:57:34 【问题描述】:

我在 Heroku 上部署了简单的博客应用程序,它在 Django=1.8.4 上运行,但我遇到了一些静态文件问题。

当打开我的应用程序时,我看到Application Error 页面,所以我尝试调试它并发现当我提交到 Heroku 时,它无法在我的静态文件夹上执行 collectstatic。其他一切正常,Heroku 向我展示 Build succeeded 但无法执行

remote: -----> Preparing static assets
remote:        Collectstatic configuration error. To debug, run:
remote:        $ heroku run python bloggy_project/manage.py collectstatic --noinput

所以 Heroku 想让我调试它。在我输入后

heroku run python bloggy_project/manage.py collectstatic --noinput

错误日志显示:

Running python bloggy_project/manage.py collectstatic --noinput on greenbloggy... up, run.4682
Traceback (most recent call last):
  File "bloggy_project/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 190, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 41, in load_command_class
    return module.Command()
  File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 32, in __init__
    self.storage.path('')
  File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py", line 48, in path
    raise ImproperlyConfigured("You're using the staticfiles app "
django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.

已经尝试用谷歌搜索答案,我现在很困惑,因为我已经阅读了一堆关于静态文件的帖子。有人可以告诉我这里出了什么问题,这样我就可以学习和理解一些东西。

我的设置.py:

"""
Django settings for bloggy_project project.

Generated by 'django-admin startproject' using Django 1.8.4.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'oi1+cyk&9g-n*nyiymkjzt6-es@!g7=edzpx+--rdsj4kw&4&3'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Local Apps
    'blog',
    # Third party apps
    'django_forms_bootstrap',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'bloggy_project.urls'

TEMPLATES = [
    
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': 
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        ,
    ,
]

WSGI_APPLICATION = 'bloggy_project.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': '',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'POST': '',
    



# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Europe/Belgrade'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, javascript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
# Static asset configuration
# Allow all host hosts/domain names for this site
ALLOWED_HOSTS = ['*']

# Srse database configuration from $DATABASE_URL
import dj_database_url

DATABASES = 'default': dj_database_url.config()

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# try to load development_settings.py if exists
try:
    from .development_settings import *
except Exception, e:
    pass

import os
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
# STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

更新:

当我注释掉STATIC_ROOT,然后做

heroku run python bloggy_project/manage.py collectstatic --noinput

现在显示错误日志,这部分令人困惑,为什么现在出现这个错误,如果我需要将STATIC_ROOT 放在我的settings.py 中?

新的错误日志:

Traceback (most recent call last):
  File "bloggy_project/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 393, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 168, in handle
    collected = self.collect()
  File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 97, in collect
    for finder in get_finders():
  File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/finders.py", line 264, in get_finders
    yield get_finder(finder_path)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
    result = user_function(*args, **kwds)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/finders.py", line 277, in get_finder
    return Finder()
  File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/finders.py", line 66, in __init__
    "The STATICFILES_DIRS setting should "
django.core.exceptions.ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting

【问题讨论】:

错误说明出了什么问题:你没有设置 STATIC_ROOT。出于某种原因,您在 settings.py 中注释掉了设置它的行。 @DanielRoseman,我更新了我的问题,做到了,注释掉了STATIC_ROOT,但现在它显示另一个错误,这部分让我很困惑,为什么现在出现这个错误?跨度> 【参考方案1】:

您收到此错误是因为您的设置变量 STATIC_ROOTSTATICFILES_DIRS 包含完全相同的路径 (os.path.join(BASE_DIR, 'static'))。这是一个无效的配置。

STATIC_ROOT 是您希望 Django 放置使用collectstatic 收集的静态文件的绝对路径。您的网络服务器将从该目录提供静态文件。

STATICFILES_DIRS 是您希望collectstatic 在收集静态文件时查看的目录列表。如果不能包含STATIC_ROOT 或其任何子目录。

尝试删除STATICFILES_DIRS 设置,看看是否有效。如果您将静态文件放在某个非标准位置,请更改设置以包含该位置。

【讨论】:

静态文件夹是我的项目根目录,其中 manage.py。在这里检查 [repository](github.com/Copser/BlogsterDj/tree/master/bloggy_project),不在家里的 atm,但我会尝试按照你的建议和结果信息。 我解决了一个静态文件问题,需要在本地做./manage collectstatic,谢谢。【参考方案2】:

也许你没有在 Django setting.py 中给出 STATIC_ROOT 路径,所以请再次检查,如果没有提供路径。给出您选择的路径,然后运行它将运行的命令。

【讨论】:

以上是关于配置不当的静态文件的主要内容,如果未能解决你的问题,请参考以下文章

Nginx配置不当可能导致的安全问题

不允许在/空静态前缀处配置不当 - Django

Django中静态文件之各个配置详解

django静态文件配置 & request对象

django静态文件配置 & request对象

Django静态文件配置