首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将Django应用程序部署到Heroku时出现集合静态错误

将Django应用程序部署到Heroku时出现集合静态错误
EN

Stack Overflow用户
提问于 2016-04-16 23:14:47
回答 19查看 75.8K关注 0票数 66

我正在尝试将Django应用程序部署到Heroku,它开始构建、下载和安装所有东西,但这就是我在收集静态文件时得到的

代码语言:javascript
运行
复制
$ python manage.py collectstatic --noinput
remote:        Traceback (most recent call last):
remote:          File "manage.py", line 10, in <module>
remote:            execute_from_command_line(sys.argv)
remote:          File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
remote:            utility.execute()
remote:          File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
remote:            self.fetch_command(subcommand).run_from_argv(self.argv)
remote:          File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
remote:            self.execute(*args, **cmd_options)
remote:          File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
remote:            output = self.handle(*args, **options)
remote:          File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 168, in handle
remote:            collected = self.collect()
remote:          File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 98, in collect
remote:            for path, storage in finder.list(self.ignore_patterns):
remote:          File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/finders.py", line 112, in list
remote:            for path in utils.get_files(storage, ignore_patterns):
remote:          File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/utils.py", line 28, in get_files
remote:            directories, files = storage.listdir(location)
remote:          File "/app/.heroku/python/lib/python2.7/site-packages/django/core/files/storage.py", line 300, in listdir
remote:            for entry in os.listdir(path):
remote:        OSError: [Errno 2] No such file or directory: '/app/blogproject/static'
remote: 
remote:  !     Error while running '$ python manage.py collectstatic --noinput'.
remote:        See traceback above for details.
remote: 
remote:        You may need to update application code to resolve this error.
remote:        Or, you can disable collectstatic for this application:
remote: 
remote:           $ heroku config:set DISABLE_COLLECTSTATIC=1
remote: 
remote:        https://devcenter.heroku.com/articles/django-assets
remote: 
remote:  !     Push rejected, failed to compile Python app
remote: 
remote: Verifying deploy...
remote: 
remote: !   Push rejected to pin-a-voyage.

这是整个settings.py文件

代码语言:javascript
运行
复制
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
import dj_database_url

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_ROOT = 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 = '*********************'

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


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',
    'custom_user',
    'django_markdown',
    'parsley',
)

#### AUTH ###

AUTH_USER_MODEL = 'custom_user.CustomUser'

AUTHENTICATION_BACKENDS = (
    'custom_user.backends.CustomUserAuth',
    'django.contrib.auth.backends.ModelBackend',
    # 'django.contrib.auth.backends.RemoteUserBackend',
)

#############

#### EMAIL ###

EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_PASSWORD = '***' #my gmail password
EMAIL_HOST_USER = 'voyage.pin@gmail.com' #my gmail username
DEFAULT_FROM_EMAIL = 'voyage.pin@gmail.com'
SERVER_EMAIL = 'voyage.pin@gmail.com'
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

##############

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 = 'blogproject.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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 = 'blogproject.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'blogproject',
        'USER': '***',
        'PASSWORD': '***',
        'HOST': 'localhost',
        'PORT': '',
    }
}


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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Update database configuration with $DATABASE_URL.
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

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

# Allow all host headers
ALLOWED_HOSTS = ['*']

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

这是项目的结构

代码语言:javascript
运行
复制
blog-project -- blog -- migrations
                     -- static
                     -- templates
             -- blogproject
             -- blogprojectenv
             -- custom_user
             -- media
             -- .git

有什么想法吗?

EN

回答 19

Stack Overflow用户

发布于 2016-08-15 19:59:29

我今天刚更新到Django 1.10,也遇到了同样的问题。您的静态设置也与我的相同。

这对我来说很有效,运行以下命令:

  1. 在部署期间禁用收集器静态

heroku config:set DISABLE_COLLECTSTATIC=1

  1. deploy

git push heroku master

  1. 运行迁移(Django1.10至少添加了一个)

heroku run python manage.py migrate

  1. 使用bower运行集合静态

heroku run 'bower install --config.interactive=false;grunt prep;python manage.py collectstatic --noinput'

  1. 支持将来部署的集合体

heroku config:unset DISABLE_COLLECTSTATIC

  1. 自行试用(可选)

heroku run python manage.py collectstatic

从现在开始,未来的部署应该会照常进行

票数 86
EN

Stack Overflow用户

发布于 2016-04-17 21:09:49

您已经将STATICFILES_DIRS配置为期望在与settings.py文件相同的目录中有一个static目录,因此请确保它不在其他位置。

另外,您在该static目录中是否有任何文件?如果你不这样做,git就不会跟踪它,所以尽管它存在于本地,它也不会存在于git中。通常的解决方案是在目录中创建一个名为.keep的空文件,这将确保git跟踪它。但是一旦你在这个目录中有了一些静态文件,那么它就不再是问题了。

票数 37
EN

Stack Overflow用户

发布于 2020-05-03 00:47:41

不要使用heroku config:set DISABLE_COLLECTSTATIC=1禁用heroku上的collectstatic。这只会隐藏错误,而不会使你的应用健康。

相反,更好的做法是理解为什么集合静态命令会失败,因为这意味着您的设置有问题。

第一步

在本地运行这两个命令:

代码语言:javascript
运行
复制
python manage.py collectstatic
python manage.py test

您应该看到一条或多条错误消息。大多数情况下,它是您必须添加到项目settings.py文件中的缺失变量(例如:STATIC_ROOT)。

有必要添加test命令,因为一些与collectstatic相关的问题只会在test中出现,比如this one

第二步

在本地修复所有错误消息后,再次推送到heroku。

故障排除

记住,你也可以直接在你的heroku虚拟机中运行命令。如果您不能在本地复制,请在heroku中运行集合体命令,并直接检查您的生产环境中发生了什么:

代码语言:javascript
运行
复制
python manage.py collectstatic --dry-run --noinput

( heroku控制台显然也是如此)

票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36665889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档