简体   繁体   English

Heroku 未检测到 static 文件的 Django 设置

[英]Heroku not detecting Django settings for static files

I am creating a project that I would like to deploy on Heroku. We are using Django and want to maintain our statics on Heroku as it is a small application.我正在创建一个项目,我想在 Heroku 上部署。我们正在使用 Django 并希望在 Heroku 上维护我们的静态,因为它是一个小应用程序。

The issue I am encountering is that when Heroku runs python manage.py collectstatic --noinput I get an error saying I haven't set my STATIC_ROOT .我遇到的问题是,当 Heroku 运行python manage.py collectstatic --noinput我收到一条错误消息,说我没有设置我的STATIC_ROOT My file structure is as follows:我的文件结构如下:

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

My settings file is我的设置文件是

"""
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--

Locally both heroku local and heroku local web work fine.本地heroku localheroku local web工作正常。 Even python manage.py collectstatic --noinput runs, however, it does create more files every time.即使python manage.py collectstatic --noinput运行,但是,它每次都会创建更多文件。 Whenever I try to push to Heroku however, I get the following error message但是,每当我尝试推送到 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

It then prints a bunch of environmental variables which don't include either BASE_DIR , STATIC_ROOT or STATIC_URL .然后它会打印出一堆不包括BASE_DIRSTATIC_ROOTSTATIC_URL的环境变量。 I suspect that this is the root cause of my issue but I am not certain how I can fix it.我怀疑这是我的问题的根本原因,但我不确定如何解决它。

I am looking for a way to add statics so that I can deploy and still have those resources available to me.我正在寻找一种方法来添加静态信息,以便我可以部署并且仍然可以使用这些资源。

It turned out that when pushing to Heroku I was pushing the wrong branch, while on the correct branch locally in order to push the correct one to a heroku remote you must use git push heroku yourbranch:master . 事实证明,当推送到Heroku时,我推送了错误的分支,而在本地正确的分支上,为了将正确的分支推送到heroku远程,则必须使用git push heroku yourbranch:master

I finally found this out once I accessed heroku via command line and looked through my code and saw the difference. 当我通过命令行访问heroku并仔细检查代码并发现其中的区别时,我终于找到了答案。 Makes sure you use the correct branch. 确保使用正确的分支。

In my case the problem was that I had not actually commited my changes to settings.py before executing git push heroku.在我的例子中,问题是在执行 git push heroku 之前,我实际上并没有提交对 settings.py 的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM