Skip to main content
 首页 » 编程设计

python之无法将我的 Django 应用程序推送到 heroku

2024年10月24日25kuangbin

你好,请原谅我的英语

这个错误很奇怪,直到现在我能够将我的 Django 应用程序和数据库推送到 heroku,这是我的配置

项目结构:

. 
├── apps 
│   ├── home 
│   │   ├── admin.py 
│   │   ├── admin.pyc 
│   │   ├── __init__.py 
│   │   ├── __init__.pyc 
│   │   ├── migrations 
│   │   ├── models.py 
│   │   ├── models.pyc 
│   │   ├── templates 
│   │   ├── tests.py 
│   │   ├── urls.py 
│   │   ├── urls.pyc 
│   │   ├── views.py 
│   │   └── views.pyc 
│   ├── __init__.py 
│   ├── __init__.pyc 
│   ├── inventario 
│   │   ├── admin.py 
│   │   ├── admin.pyc 
│   │   ├── forms.py 
│   │   ├── forms.pyc 
│   │   ├── __init__.py 
│   │   ├── __init__.pyc 
│   │   ├── migrations 
│   │   ├── models.py 
│   │   ├── models.pyc 
│   │   ├── templates 
│   │   ├── tests.py 
│   │   ├── urls.py 
│   │   ├── urls.pyc 
│   │   ├── views.py 
│   │   └── views.pyc 
│   ├── ordenes_trabajo 
│   │   ├── admin.py 
│   │   ├── admin.pyc 
│   │   ├── forms.py 
│   │   ├── forms.pyc 
│   │   ├── __init__.py 
│   │   ├── __init__.pyc 
│   │   ├── migrations 
│   │   ├── models.py 
│   │   ├── models.pyc 
│   │   ├── templates 
│   │   ├── tests.py 
│   │   ├── urls.py 
│   │   ├── urls.pyc 
│   │   ├── views.py 
│   │   └── views.pyc 
│   ├── sucursales 
│   │   ├── admin.py 
│   │   ├── admin.pyc 
│   │   ├── forms.py 
│   │   ├── forms.pyc 
│   │   ├── __init__.py 
│   │   ├── __init__.pyc 
│   │   ├── migrations 
│   │   ├── models.py 
│   │   ├── models.pyc 
│   │   ├── templates 
│   │   ├── tests.py 
│   │   ├── urls.py 
│   │   ├── urls.pyc 
│   │   ├── views.py 
│   │   └── views.pyc 
│   ├── usuarios 
│   │   ├── admin.py 
│   │   ├── admin.pyc 
│   │   ├── forms.py 
│   │   ├── forms.pyc 
│   │   ├── __init__.py 
│   │   ├── __init__.pyc 
│   │   ├── migrations 
│   │   ├── models.py 
│   │   ├── models.pyc 
│   │   ├── templates 
│   │   ├── tests.py 
│   │   ├── urls.py 
│   │   ├── urls.pyc 
│   │   ├── views.py 
│   │   └── views.pyc 
│   └── ventas_cotizaciones 
│       ├── admin.py 
│       ├── admin.pyc 
│       ├── forms.py 
│       ├── forms.pyc 
│       ├── htmltopdf.py 
│       ├── htmltopdf.pyc 
│       ├── __init__.py 
│       ├── __init__.pyc 
│       ├── migrations 
│       ├── models.py 
│       ├── models.pyc 
│       ├── templates 
│       ├── tests.py 
│       ├── urls.py 
│       ├── urls.pyc 
│       ├── views.py 
│       └── views.pyc 
├ 
├── __init__.py 
├── manage.py 
├── prerequirements_install.txt 
├── Procfile 
├── proyecto_www 
│   ├── db.sqlite3 
│   ├── __init__.py 
│   ├ 
│   ├── README.md 
│   ├── settings 
│   │   ├── base.py 
│   │   ├── base.pyc 
│   │   ├── __init__.py 
│   │   ├ 
│   │   ├── local.py 
│   │   ├ 
│   │   ├── production.py 
│   │   ├── staging.py 
│   │   └── staging.pyc 
│   ├── urls.py 
│   ├ 
│   ├── wsgi.py 
│   └ 
├── requirements.txt 
├── static 

我这样拆分设置:

设置/base.py

from unipath import Path 
import os 
 
BASE_DIR = Path(__file__).ancestor(3) 
# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ 
 
# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = 'THE SECRET KEY OF MY APP' 
 
# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = True 
 
 
ALLOWED_HOSTS = ['*'] 
 
 
# Application definition 
 
DJANGO_APPS = ( 
   ... 
) 
 
 
LOCAL_APPS = ( 
   ... 
) 
 
 
THIRD_PARTY_APPS = ( 
  ... 
) 
 
INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS + THIRD_PARTY_APPS 
 
MIDDLEWARE_CLASSES = ( 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
    'django.middleware.security.SecurityMiddleware', 
    'corsheaders.middleware.CorsMiddleware', 
    'django.middleware.common.CommonMiddleware', 
) 
 
ROOT_URLCONF = 'proyecto_www.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', 
            ], 
        }, 
    }, 
] 
 
WSGI_APPLICATION = 'proyecto_www.wsgi.application' 
 
STATIC_URL = '/static/' 
STATICFILES_DIRS=(BASE_DIR,'static',) 
MEDIA_URL = '/media/' 
MEDIA_ROOT = BASE_DIR.child('media') 
# Internationalization 
# https://docs.djangoproject.com/en/1.8/topics/i18n/ 
 
LANGUAGE_CODE = 'es' 
 
TIME_ZONE = 'UTC' 
 
USE_I18N = True 
 
USE_L10N = True 
 
USE_TZ = True 
 
 
CORS_ORIGIN_ALLOW_ALL = True 
 
CORS_ALLOW_METHODS = ( 
       ... 
    ) 

还有我用于部署的 settings/staging.py

 from .base import * 
 
    DEBUG_TOOLBAR_PATCH_SETTINGS = False 
    DEBUG = True 
    TEMPLATE_DEBUG = True 
 
    # Database 
    # https://docs.djangoproject.com/en/1.8/ref/settings/#databases 
 
    DATABASES = { 
       ... 
 
    } 
 
 
# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.8/howto/static-files/ 
 
STATIC_ROOT = 'staticfiles' 
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' 

我的 Procfile 是:

web: gunicorn proyecto_www.wsgi --log-file - 

我的 wsgi.py 是:

import os 
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proyecto_www.settings.staging") 
from django.core.wsgi import get_wsgi_application 
from whitenoise.django import DjangoWhiteNoise 
application = DjangoWhiteNoise(get_wsgi_application()) 

最初的错误是“SECRET_KEY 设置不能为空”

所以我按照这里的答案 Django. Using multiple settings files with Heroku

然后使用命令:

heroku config:set DJANGO_SETTINGS_MODULE=proyecto_www.settings.staging 

现在我收到这个错误:

 remote:        Successfully installed BeautifulSoup-3.2.1 Django-1.8.4 Pillow-3.0.0 PyPDF2-1.25.1 Unipath-1.1 dj-database-url-0.3.0 dj-static-0.0.6 django-cors-headers-1.1.0 django-datatable-view-0.8.3 django-toolbelt-0.0.1 gunicorn-19.4.1 html5lib-0.9999999 psycopg2-2.6.1 python-dateutil-2.4.2 reportlab-3.2.0 six-1.10.0 static3-0.6.1 wheel-0.24.0 whitenoise-2.0.6 xhtml2pdf-0.0.6 xlwt-0.7.4 
remote:  
remote:      $ python manage.py collectstatic --noinput 
remote:        Post-processing 'tmp/cache/tmp_app_dir/vendor/ruby-2.3.0/lib/ruby/2.3.0/rdoc/generator/template/darkfish/css/rdoc.css' failed! 
remote:        Traceback (most recent call last): 
remote:          File "manage.py", line 10, in <module> 
remote:            execute_from_command_line(sys.argv) 
remote:          File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line 
remote:            utility.execute() 
remote:          File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute 
remote:            self.fetch_command(subcommand).run_from_argv(self.argv) 
remote:          File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 393, in run_from_argv 
remote:            self.execute(*args, **cmd_options) 
remote:          File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 444, in execute 
remote:            output = self.handle(*args, **options) 
remote:          File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 168, in handle 
remote:            collected = self.collect() 
remote:          File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 120, in collect 
remote:            raise processed 
remote:        whitenoise.django.MissingFileError: The file 'tmp/cache/tmp_app_dir/vendor/ruby-2.3.0/lib/ruby/2.3.0/rdoc/generator/template/darkfish/css/images/wrench_orange.png' could not be found with <whitenoise.django.GzipManifestStaticFilesStorage object at 0x7fe55e8ec4d0>. 
remote:        The CSS file 'tmp/cache/tmp_app_dir/vendor/ruby-2.3.0/lib/ruby/2.3.0/rdoc/generator/template/darkfish/css/rdoc.css' references a file which could not be found: 
remote:          tmp/cache/tmp_app_dir/vendor/ruby-2.3.0/lib/ruby/2.3.0/rdoc/generator/template/darkfish/css/images/wrench_orange.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:        http://devcenter.heroku.com/articles/django-assets 
remote:  
remote:  !     Push rejected, failed to compile Python app 

请您参考如下方法:

解决方案是从 settings/staging.py 中删除这一行

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' 

然后像这样更改 wsgi.py:

import os 
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proyecto_www.settings.staging") 
from django.core.wsgi import get_wsgi_application 
from dj_static import Cling 
application = Cling(get_wsgi_application()) 

dj_static 库带有 django-toolbelt 或 dj-static