使用 Django REST 框架将 AngularJS 应用程序部署到 Heroku

Deploying AngularJS app with Django REST framework to Heroku

我一直在本地开发 AngularJS 前端 Django REST 后端项目,我想将它部署到 Heroku。但是,当我尝试推送时它总是给我错误,

我已经按照 here 上的步骤进行操作,这帮助我之前部署了仅 Django 的应用程序。

下面是我的 heroku logs.

的输出
8" dyno= connect= service= status=503 bytes=
2015-04-11T20:51:24.680164+00:00 heroku[api]: Deploy efbd3fb by me@example.com
2015-04-11T20:51:24.680164+00:00 heroku[api]: Release v5 created by me@example.com
2015-04-11T20:51:25.010989+00:00 heroku[web.1]: State changed from crashed to starting
2015-04-11T20:51:30.109140+00:00 heroku[web.1]: Starting process with command `python manage.py collectstatic --noinput; gunicorn myApp.wsgi --log-file -`
2015-04-11T20:51:31.795813+00:00 app[web.1]: Detected 512 MB available memory, 512 MB limit per process (WEB_MEMORY)
2015-04-11T20:51:31.795838+00:00 app[web.1]: Recommending WEB_CONCURRENCY=1
2015-04-11T20:51:31.824251+00:00 app[web.1]: Traceback (most recent call last):
2015-04-11T20:51:31.824257+00:00 app[web.1]:   File "manage.py", line 8, in <module>
2015-04-11T20:51:31.824267+00:00 app[web.1]:     from django.core.management import execute_from_command_line
2015-04-11T20:51:31.824296+00:00 app[web.1]: ImportError: No module named django.core.management
2015-04-11T20:51:31.827843+00:00 app[web.1]: bash: gunicorn: command not found
2015-04-11T20:51:32.656017+00:00 heroku[web.1]: Process exited with status 127
2015-04-11T20:51:32.672886+00:00 heroku[web.1]: State changed from starting to crashed
2015-04-11T20:51:32.672886+00:00 heroku[web.1]: State changed from crashed to starting
2015-04-11T20:51:36.675087+00:00 heroku[web.1]: Starting process with command `python manage.py collectstatic --noinput; gunicorn myApp.wsgi --log-file -`
2015-04-11T20:51:38.446374+00:00 app[web.1]: bash: gunicorn: command not found
2015-04-11T20:51:38.420704+00:00 app[web.1]: Detected 512 MB available memory, 512 MB limit per process (WEB_MEMORY)
2015-04-11T20:51:38.420729+00:00 app[web.1]: Recommending WEB_CONCURRENCY=1
2015-04-11T20:51:38.442039+00:00 app[web.1]:   File "manage.py", line 8, in <module>
2015-04-11T20:51:38.442033+00:00 app[web.1]: Traceback (most recent call last):
2015-04-11T20:51:38.443526+00:00 app[web.1]: ImportError: No module named django.core.management
2015-04-11T20:51:38.442047+00:00 app[web.1]:     from django.core.management import execute_from_command_line
2015-04-11T20:51:39.265192+00:00 heroku[web.1]: Process exited with status 127
2015-04-11T20:51:39.287328+00:00 heroku[web.1]: State changed from starting to crashed
2015-04-11T20:52:10.960135+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=myapp.herokuapp.com request_id=afbb960d-eae4-4891-a885-d4a7e3880f1f fwd="64.247.79.248" dyno= connect= service= status=503 bytes=
2015-04-11T20:52:11.321003+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=myapp.herokuapp.com request_id=de3242f1-7dda-4cc5-8b35-6d7c77392151 fwd="64.247.79.248" dyno= connect= service= status=503 bytes=

Heroku 似乎因为使用哪个服务器(Node.js 与 Django)之间的歧义而感到困惑并且无法解决它。 Heroku 好像找不到 Django。

我的项目的基本大纲是基于这个例子:https://github.com/brwr/thinkster-django-angular.

我的设置文件如下:

"""
Django settings for my project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = SUPER_SECRET

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get('DEBUG', True)

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'debug_toolbar',
    'rest_framework',
    'compressor',
    'authentication',
)

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',
)

ROOT_URLCONF = 'myApp.urls'

WSGI_APPLICATION = 'myApp.wsgi.application'


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

import dj_database_url

DATABASES = {
    'default': dj_database_url.config(
        default='sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3')
    )
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/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.7/howto/static-files/

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

STATICFILES_DIRS = (
#    os.path.join(BASE_DIR, 'dist/static'),
    os.path.join(BASE_DIR, 'static'),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'compressor.finders.CompressorFinder',
)

COMPRESS_ENABLED = os.environ.get('COMPRESS_ENABLED', False)

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
    )
}

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers
ALLOWED_HOSTS = ['*']

AUTH_USER_MODEL = 'authentication.Account'


# Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES['default'] =  dj_database_url.config()

# Enable Connection Pooling
DATABASES['default']['ENGINE'] = 'django_postgrespool'


# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

如果需要更多信息,我可以添加。

谢谢, 埃里普

编辑

我在推送到 Heroku 时注意到了这一点:

它肯定被识别为 Node.js 后端而不是 Django,因此,它不是从我的 requirements.txt 安装的。我怎样才能改变这个?我尝试了 heroku config:set BUILDPACK_URL=https://github.com/heroku/heroku-buildpack-python,但似乎没有用。

你的virtualenv好像没有安装Django

你有没有忘记运行pip install django-toolbelt

我不明白为什么在 Django 项目中需要 Node - Angular 或 DRF 不需要 - 但链接项目中的说明提到将 buildpack 设置为自定义 "multi"一个:

heroku config:set BUILDPACK_URL=https://github.com/ddollar/heroku-buildpack-multi.git