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

时间:2015-04-11 21:11:42

标签: django angularjs heroku django-rest-framework

我一直在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')

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

谢谢, erip

修改

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

Node.js

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

2 个答案:

答案 0 :(得分:1)

我不明白你为什么在Django项目中需要Node - Angular或DRF不需要它 - 但是链接项目中的说明提到将buildpack设置为自定义的“多”项:

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

答案 1 :(得分:0)

好像你没有在你的virtualenv中安装Django

您忘了运行pip install django-toolbelt