简体   繁体   English

使用 Heroku 在 Django 应用程序中部署时出错

[英]Errors with deployment in Django app using Heroku

I have been trying to deploy my app all day, but have had no luck.我一整天都在尝试部署我的应用程序,但没有成功。 I followed the steps on here .我按照这里的步骤操作。 It's hard for me to interpret the logs, so I was hoping I could get some help here.我很难解释日志,所以我希望我能在这里得到一些帮助。

settings.py设置.py

    """
Django settings for GingerEats 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/
"""


import os
import dj_database_url
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
ACTUAL_DIR = os.path.dirname(__file__)
TEMPLATE_PATH = os.path.join(ACTUAL_DIR, 'templates')
STATIC_PATH = os.path.join(ACTUAL_DIR, 'static')


SECRET_KEY = 'SECRET_KEY'


DEBUG = False

TEMPLATE_DEBUG = True

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    TEMPLATE_PATH,
)

ALLOWED_HOSTS = ['*']

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


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'GingerBites'
)

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 = 'GingerEats.urls'

WSGI_APPLICATION = 'GingerEats.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
import dj_database_url
DATABASES = {'default': dj_database_url.config()}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/Los_Angeles'

USE_I18N = True

USE_L10N = True

USE_TZ = True




STATIC_URL = '/static/'

STATICFILES_DIRS = (
    STATIC_PATH,
)

Procfile配置文件

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

requirements.txt要求.txt

beautifulsoup4==4.4.0
dateutils==0.6.6
dj-database-url==0.3.0
dj-static==0.0.6
Django==1.8.3
django-toolbelt==0.0.1
gunicorn==19.3.0
Pillow==2.9.0
psycopg2==2.6.1
python-dateutil==2.4.2
pytz==2015.4
six==1.9.0
static3==0.6.1
wheel==0.24.0

From app helper file来自应用程序帮助文件

from bs4 import BeautifulSoup

heroku logs Heroku 日志

2015-07-20T09:48:46.585197+00:00 heroku[slug-compiler]: Slug compilation started
2015-07-20T09:48:46.585221+00:00 heroku[slug-compiler]: Slug compilation finished
2015-07-20T09:48:46.632808+00:00 heroku[web.1]: State changed from up to starting
2015-07-20T09:48:49.329911+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2015-07-20T09:48:50.033795+00:00 app[web.1]: [2015-07-20 02:48:50 +0000] [10] [INFO] Worker exiting (pid: 10)
2015-07-20T09:48:50.063556+00:00 app[web.1]: [2015-07-20 09:48:50 +0000] [3] [INFO] Shutting down: Master
2015-07-20T09:48:50.019993+00:00 app[web.1]: [2015-07-20 02:48:50 +0000] [9] [INFO] Worker exiting (pid: 9)
2015-07-20T09:48:50.040626+00:00 app[web.1]: [2015-07-20 09:48:50 +0000] [3] [INFO] Handling signal: term
2015-07-20T09:48:50.370142+00:00 heroku[web.1]: Starting process with command `gunicorn GingerEats.wsgi --log-file -`
2015-07-20T09:48:50.947622+00:00 heroku[web.1]: Process exited with status 0
2015-07-20T09:48:53.132305+00:00 heroku[web.1]: State changed from starting to up
2015-07-20T09:48:53.036580+00:00 app[web.1]: [2015-07-20 09:48:53 +0000] [3] [INFO] Starting gunicorn 19.3.0
2015-07-20T09:48:53.043597+00:00 app[web.1]: [2015-07-20 09:48:53 +0000] [3] [INFO] Using worker: sync
2015-07-20T09:48:53.158484+00:00 app[web.1]: [2015-07-20 09:48:53 +0000] [10] [INFO] Booting worker with pid: 10
2015-07-20T09:48:53.037329+00:00 app[web.1]: [2015-07-20 09:48:53 +0000] [3] [INFO] Listening at: http://0.0.0.0:16584 (3)
2015-07-20T09:48:53.074698+00:00 app[web.1]: [2015-07-20 09:48:53 +0000] [9] [INFO] Booting worker with pid: 9
2015-07-20T09:48:56.052969+00:00 heroku[router]: at=info method=GET path="/" host=young-river-3159.herokuapp.com request_id=7bbae61c-1677-443e-a661-e5e49c8298fd fwd="73.189.127.134" dyno=web.1 connect=1ms service=475ms status=500 bytes=239
2015-07-20T09:55:45.893669+00:00 heroku[router]: at=info method=GET path="/" host=young-river-3159.herokuapp.com request_id=fa2f81a3-fa90-41c3-87f5-d1e7b0402fe9 fwd="73.189.127.134" dyno=web.1 connect=3ms service=32ms status=500 bytes=239
2015-07-20T09:57:53.360261+00:00 heroku[api]: Starting process with command `python manage.py collectstatic --noinput` by 
2015-07-20T09:57:58.504151+00:00 heroku[run.9993]: Awaiting client
2015-07-20T09:57:58.545153+00:00 heroku[run.9993]: Starting process with command `python manage.py collectstatic --noinput`
2015-07-20T09:57:58.884863+00:00 heroku[run.9993]: State changed from starting to up
2015-07-20T09:58:01.531091+00:00 heroku[run.9993]: State changed from up to complete
2015-07-20T09:58:01.516810+00:00 heroku[run.9993]: Process exited with status 1
2015-07-20T10:05:23.378880+00:00 heroku[slug-compiler]: Slug compilation started
2015-07-20T10:05:23.378899+00:00 heroku[slug-compiler]: Slug compilation finished
2015-07-20T10:05:23.321985+00:00 heroku[api]: Deploy 36e0a00 by me@gmail.com
2015-07-20T10:05:23.321985+00:00 heroku[api]: Release v23 created by me@gmail.com
2015-07-20T10:05:23.484287+00:00 heroku[web.1]: State changed from up to starting
2015-07-20T10:05:26.662103+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2015-07-20T10:05:27.376506+00:00 app[web.1]: [2015-07-20 10:05:27 +0000] [3] [INFO] Handling signal: term
2015-07-20T10:05:27.377900+00:00 app[web.1]: [2015-07-20 03:05:27 +0000] [9] [INFO] Worker exiting (pid: 9)
2015-07-20T10:05:27.377911+00:00 app[web.1]: [2015-07-20 03:05:27 +0000] [10] [INFO] Worker exiting (pid: 10)
2015-07-20T10:05:27.795568+00:00 app[web.1]: [2015-07-20 10:05:27 +0000] [3] [INFO] Shutting down: Master
2015-07-20T10:05:28.751407+00:00 heroku[web.1]: Process exited with status 0
2015-07-20T10:05:35.131870+00:00 heroku[web.1]: Starting process with command `gunicorn GingerEats.wsgi --log-file -`
2015-07-20T10:05:38.259027+00:00 app[web.1]: [2015-07-20 10:05:38 +0000] [3] [INFO] Listening at: http://0.0.0.0:13138 (3)
2015-07-20T10:05:38.259151+00:00 app[web.1]: [2015-07-20 10:05:38 +0000] [3] [INFO] Using worker: sync
2015-07-20T10:05:38.281227+00:00 app[web.1]: [2015-07-20 10:05:38 +0000] [9] [INFO] Booting worker with pid: 9
2015-07-20T10:05:38.258301+00:00 app[web.1]: [2015-07-20 10:05:38 +0000] [3] [INFO] Starting gunicorn 19.3.0
2015-07-20T10:05:38.311885+00:00 app[web.1]: [2015-07-20 10:05:38 +0000] [10] [INFO] Booting worker with pid: 10
2015-07-20T10:05:39.141716+00:00 heroku[web.1]: State changed from starting to up
2015-07-20T10:05:48.344166+00:00 heroku[router]: at=info method=GET path="/" host=young-river-3159.herokuapp.com request_id=5ca6f7b1-fc15-45e1-9cd5-6d25e6f1c7f2 fwd="73.189.127.134" dyno=web.1 connect=0ms service=274ms status=500 bytes=239


**Actual push to heroku**

    git push heroku master
    Counting objects: 4, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (4/4), done.
    Writing objects: 100% (4/4), 357 bytes | 0 bytes/s, done.
    Total 4 (delta 3), reused 0 (delta 0)
    remote: Compressing source files... done.
    remote: Building source:
    remote: 
    remote: -----> Removing .DS_Store files
    remote: -----> Python app detected
    remote: -----> Installing dependencies with pip
    remote: You are using pip version 7.0.3, however version 7.1.0 is available.
    remote: You should consider upgrading via the 'pip install --upgrade pip' command.
    remote: 
    remote: -----> Preparing static assets
    remote:        Collectstatic configuration error. To debug, run:
    remote:        $ heroku run python manage.py collectstatic --noinput
    remote: 
    remote: -----> Discovering process types
    remote:        Procfile declares types -> web
    remote: 
    remote: -----> Compressing... done, 37.9MB
    remote: -----> Launching... done, v22
    remote:        https://young-river-3159.herokuapp.com/ deployed to Heroku
    remote: 
    remote:  !   Cedar-10 will reach end-of-life on November 4th, 2015.
    remote:  !   Upgrade to Cedar-14 at your earliest convenience.
    remote:  !   For more information, check out the following Dev Center article:
    remote:  !   https://devcenter.heroku.com/articles/cedar-14-migration
    remote: 
    remote: Verifying deploy.... done.
    To https://git.heroku.com/young-river-3159.git
       0e95162..4bc34e8  master -> master

**Static Assets debug**
    heroku run python manage.py collectstatic --noinput

    Running `python manage.py collectstatic --noinput` attached to terminal... up, run.9993
    Traceback (most recent call last):
      File "manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
        utility.execute()
      File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 190, in fetch_command
        klass = load_command_class(app_name, subcommand)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 41, in load_command_class
        return module.Command()
      File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 32, in __init__
        self.storage.path('')
      File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py", line 48, in path
        raise ImproperlyConfigured("You're using the staticfiles app "
    django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.

Any help would be so much appreciated. This has been so difficult to overcome. Procfile and requirements.txt are in root directory. This all results in a 
Server Error (500).

The error log you're getting gives a good description of the problem:您收到的错误日志很好地描述了问题:

...ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path. ...ImproperlyConfigured:您正在使用 staticfiles 应用程序,而没有将 STATIC_ROOT 设置设置为文件系统路径。

You're running the manage.py collectstatic command as part of deployment.您正在运行manage.py collectstatic命令作为部署的一部分。 This should collect all your static files from your various STATICFILES_DIRS and put them into one folder: STATIC_ROOT for your webserver (Apache or Nginx) to serve them.这应该从您的各种STATICFILES_DIRS收集所有静态文件并将它们放入一个文件夹: STATIC_ROOT供您的网络服务器(Apache 或 Nginx)为它们提供服务。

You need to select a folder for STATIC_ROOT so that collectstatic can put your static files into that folder, per the documentation here .根据此处文档,您需要为STATIC_ROOT选择一个文件夹,以便collectstatic可以将您的静态文件放入该文件夹中。

You'll also have to make sure the static files directories are properly configured and that your webserver is properly calling them, but the above is the current crux of the problem.您还必须确保正确配置静态文件目录并且您的网络服务器正确调用它们,但以上是当前问题的症结所在。

Well, you are using debug mode false.好吧,您正在使用调试模式 false。 The error occurs because you haven't correctly set static files directory for application.发生错误是因为您没有正确设置应用程序的静态文件目录。 In your settings.py paste the code below.在您的settings.py粘贴以下代码。

#default static url
STATIC_URL = '/static/'

#this when debug is True, the project should look where static folder located, set STATICFILES_DIRS accordingly:
if DEBUG:
   STATICFILES_DIRS = [
   os.path.join(BASE_DIR, 'static'),
   ]
#when you change environemnt to production, STATIC_ROOT should be given. 
else:
   STATIC_ROOT = os.path.join(BASE_DIR,'static')

#for media files add followings:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM