Heroku未检测到静态文件的Django设置

时间:2019-01-23 12:44:52

标签: python django heroku

我正在创建一个要在Heroku上部署的项目。我们正在使用Django,并希望在Heroku上维护我们的静态信息,因为它是一个小型应用程序。

我遇到的问题是,当Heroku运行python manage.py collectstatic --noinput时,我收到一条错误消息,提示我尚未设置STATIC_ROOT。我的文件结构如下:

├── htmlcov
├── mysite
│   ├── settings.py
│   ├── wsgi.py
│   ├── urls.py
│   └── random.py
│
├── staticfiles
├── tweetmood
│   ├── migrations
│   └── tests
│
├── Procfile
├── runtime.txt
└── manage.py

我的设置文件是

"""
Django settings for mysite project.

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

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

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

import os
import django_heroku

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'MY_SUPER_SECRET_KEY'

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

ALLOWED_HOSTS = ['0.0.0.0', 'localhost', 'https://my-project-name.herokuapp.com']

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'tweetmood',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mysite.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 = 'mysite.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

# Configure Django App for Heroku.
django_heroku.settings(locals())

Procfile:

web: gunicorn mysite.wsgi —-log-file--

在本地heroku localheroku local web都可以正常工作。但是,即使python manage.py collectstatic --noinput运行,它每次也会创建更多文件。每当我尝试推送到Heroku时,都会收到以下错误消息

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.

  !     Error while running '$ python manage.py collectstatic --noinput'.
        See traceback above for details.

        You may need to update application code to resolve this error.
        Or, you can disable collectstatic for this application:

           $ heroku config:set DISABLE_COLLECTSTATIC=1

        https://devcenter.heroku.com/articles/django-assets

 ****** Collectstatic environment variables:ttps://devcenter.heroku.com/articles/django-assets

然后打印一堆不包含BASE_DIRSTATIC_ROOTSTATIC_URL的环境变量。我怀疑这是问题的根本原因,但我不确定如何解决。

我正在寻找一种添加静态变量的方法,以便我可以部署并仍然可以使用那些资源。

1 个答案:

答案 0 :(得分:0)

事实证明,当推送到Heroku时,我推送了错误的分支,而在本地正确的分支上,要将正确的分支推送到heroku远程服务器,则必须使用git push heroku yourbranch:master

当我通过命令行访问heroku并仔细检查代码并发现其中的区别时,我终于找到了答案。确保使用正确的分支。