【问题标题】:Deploying AngularJS app with Django REST framework to Heroku将带有 Django REST 框架的 AngularJS 应用程序部署到 Heroku
【发布时间】:2015-06-17 10:50:28
【问题描述】:

我一直在本地开发 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 后端而不是 Django,因此,它不是从我的requirements.txt 安装的。我怎样才能改变这个?我试过heroku config:set BUILDPACK_URL=https://github.com/heroku/heroku-buildpack-python,但似乎没有用。

【问题讨论】:

  • 你的项目根目录下有requirements.txt 文件吗? AFAIK,这是 Heroku 用于检测 Python 应用程序的标准。
  • @niconoe 是的,在根目录中有一个requirements.txt。它同时检测 django 和 Node。

标签: django angularjs heroku django-rest-framework


【解决方案1】:

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

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

【讨论】:

  • 我按照这些说明得到了Push rejected, no Cedar-supported app detected
【解决方案2】:

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

你忘了跑pip install django-toolbelt

【讨论】:

  • 我已经有 django-toolbelt,但 Heroku 好像没有。
  • 你使用的是哪个版本的 django?
  • 我使用的是 Django 1.7.1
  • 你有 requirements.txt 文件吗?
猜你喜欢
  • 2019-03-04
  • 2021-08-10
  • 2016-11-16
  • 1970-01-01
  • 2021-12-08
  • 2017-08-27
  • 2022-01-25
相关资源
最近更新 更多