简体   繁体   中英

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.

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 . 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. Even python manage.py collectstatic --noinput runs, however, it does create more files every time. Whenever I try to push to Heroku however, I get the following error message

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

I finally found this out once I accessed heroku via command line and looked through my code and saw the difference. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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