【问题标题】:How can i deploy Django to Heroku with static files如何使用静态文件将 Django 部署到 Heroku
【发布时间】:2021-08-22 21:47:44
【问题描述】:

我想在 heroku 上发布我的 django 应用程序,但我收到了我提到的错误。我研究并尝试了很多有关它的方法,但我无法获得积极的结果。

remote: -----> $ python manage.py collectstatic --noinput
remote:        Post-processing 'css/semantic.min.css' failed!
remote:        Traceback (most recent call last):
remote:          File "manage.py", line 22, in <module>
remote:            main()
remote:          File "manage.py", line 18, in main
remote:            execute_from_command_line(sys.argv)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
remote:            utility.execute()
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
remote:            self.fetch_command(subcommand).run_from_argv(self.argv)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/base.py", line 330, in run_from_argv
remote:            self.execute(*args, **cmd_options)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/base.py", line 371, in execute
remote:            output = self.handle(*args, **options)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 194, in handle
remote:            collected = self.collect()
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 138, in collect
remote:            raise processed
remote:        whitenoise.storage.MissingFileError: The file 'css/themes/default/assets/images/flags.png' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0x7f96f75bdf90>.
remote:        The CSS file 'css/semantic.min.css' references a file which could not be found:
remote:          css/themes/default/assets/images/flags.png
remote:        Please check the URL references in this CSS file, particularly any
remote:        relative paths which might be pointing to the wrong location.
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:  !     Push rejected, failed to compile Python app.
remote:
remote:  !     Push failed
remote: Verifying deploy...
remote:
remote: !       Push rejected to alice-cern.
remote:
To https://git.heroku.com/alice-cern.git

我使用命令heroku config:set DISABLE_COLLECTSTATIC=1 解决了这个问题,但当时我的css 和js 文件不起作用。

settings.py

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '83ggbd#=c+m56_3p=+@m&mu-xrwm28&l^6@jvz+@z&)7k!ya)^'

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

ALLOWED_HOSTS = ['alice-cern.herokuapp.com']

X_FRAME_OPTIONS = 'SAMEORIGIN'

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'ckeditor',
    'articles',
    'crispy_forms',
    'announcement',
    'meeting_reports',
    'shifts',
    'main',
    'phonenumber_field',
    'researchs',
]

MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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 = 'CERN.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        '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',
                'CERN.context_processors.last_three_articles',
            ],

            'libraries':{
                'converter' : 'shifts.templatetags.converter',
            }
        },
    },
]

WSGI_APPLICATION = 'CERN.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.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/3.1/topics/i18n/

LANGUAGE_CODE = 'tr'

TIME_ZONE = 'Europe/Istanbul'

USE_I18N = True

USE_L10N = True

USE_TZ = True



# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

STATIC_ROOT =  BASE_DIR / 'staticfiles'
STATIC_URL = '/static/'


STATICFILES_DIRS = (
BASE_DIR / 'static',
)

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')

CKEDITOR_CONFIGS = {
    "default": {
        "removePlugins": "stylesheetparser",
        "allowedContent" : True,
        "width" : "100%",
    }
}


import django_heroku
django_heroku.settings(locals())

有人告诉我,settings.py 文件底部的两行代码可以解决整个问题,但没有帮助。我还是把那部分留在那里了。

【问题讨论】:

    标签: python python-3.x django heroku


    【解决方案1】:

    我认为你应该先在项目本地运行“python manage.py collectstatic”,然后提交更改,推送到github并再次部署。

    【讨论】:

    • 我尝试这样做,但它也在本地机器上给出了同样的错误。
    猜你喜欢
    • 2016-04-16
    • 2014-06-08
    • 2016-03-11
    • 2016-12-23
    • 2020-05-19
    • 1970-01-01
    • 2020-12-17
    • 2021-07-23
    • 2020-11-22
    相关资源
    最近更新 更多