配置不正确的静态文件 [英] ImproperlyConfigured staticfiles

查看:69
本文介绍了配置不正确的静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have deploy simple blog application on Heroku, it run on Django=1.8.4, and I'm having some issues whit static files.

打开我的应用程序时,我会看到 Application Error 页面,因此我尝试对其进行调试,并在提交给Heroku时发现它无法在我的静态文件夹上执行collectstatic。其他一切正常,Heroku向我显示构建成功但无法执行

When open mine application I see Application Error page, so I have try to debug it and find out that when I commit to Heroku it can't preform collectstatic on mine static folder. Everything else is working and Heroku is showing me that Build succeeded but can not preform

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

所以Heroku要我调试它。键入

so Heroku want me to debug it. After I type

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.

曾经尝试过用Google搜索答案,现在我很困惑,因为我看过很多帖子关于静态文件。有人可以告诉我这里出了什么问题,以便我可以学习和理解。

Have tried to google for the answer, I'm very confused now because I have read bunch of posts about static files. Can someone show me what is wrong here so I can learn and understand something.

矿山settings.py:

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

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

Error log now show, and this part is rely confusing, why this error now, if I need to put STATIC_ROOT in mine 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 STATICFILES_DIRS 包含完全相同的路径( os.path.join(BASE_DIR,静态))。这是无效的配置。

You are getting this error because your settings variables STATIC_ROOT and STATICFILES_DIRS contain exactly the same path (os.path.join(BASE_DIR, 'static')). This is an invalid configuration.

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

STATIC_ROOT is the absolute path to where you want Django to put the static files that are collected using collectstatic. Your web server will serve static files from this directory.

STATICFILES_DIRS 是您要在其中添加目录的列表 collectstatic 来收集静态文件。如果不能包含 STATIC_ROOT 或其任何子目录。

STATICFILES_DIRS is a list of directories where you want collectstatic to look when collecting the static files. If cannot include STATIC_ROOT or any subdirectory of it.

请尝试删除 STATICFILES_DIRS 设置,看看是否可行。如果您已将静态文件放置在某些非标准位置,请更改设置以包括该位置。

Try removing the STATICFILES_DIRS setting and see if that works. If you have put static files in some non-standard location then change the setting to include that location.

这篇关于配置不正确的静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆