簡體   English   中英

Heroku 未檢測到 static 文件的 Django 設置

[英]Heroku not detecting Django settings for static files

我正在創建一個項目,我想在 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())

簡介:

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的環境變量。 我懷疑這是我的問題的根本原因,但我不確定如何解決它。

我正在尋找一種方法來添加靜態信息,以便我可以部署並且仍然可以使用這些資源。

事實證明,當推送到Heroku時,我推送了錯誤的分支,而在本地正確的分支上,為了將正確的分支推送到heroku遠程,則必須使用git push heroku yourbranch:master

當我通過命令行訪問heroku並仔細檢查代碼並發現其中的區別時,我終於找到了答案。 確保使用正確的分支。

在我的例子中,問題是在執行 git push heroku 之前,我實際上並沒有提交對 settings.py 的更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM