我正在尝试使用 heroku 部署 django 应用程序但出现错误?

时间:2021-06-12 09:58:05

标签: django heroku

构建成功并且它产生应用程序错误,我已经按照建议设置了主机名和 debug=False 但它仍然导致打开浏览器窗口时出错,我是 heroku 新手,所以请建议需要做什么让它发挥作用

我的 settings.py

"""
Django settings for djangoTut project.

Generated by 'django-admin startproject' using Django 3.2.4.

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

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

from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

print(BASE_DIR)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

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

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['djangoblog-project.herokuapp.com']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog.apps.BlogConfig',
    'users.apps.UsersConfig',
    'crispy_forms',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'djangoTut.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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 = 'djangoTut.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'DATABASE' : 'DjangoProject',
        'NAME': 'DjangoProject',
        'USER' : 'myprojectuser',
        'PASSWORD' : 'Akbar@123456',
        'HOST': 'localhost',
        'PORT': '',
        # 'default-character-se' : 'utf8',
       # 'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


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

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

STATICFILES_DIRS = [
   
]

CRISPY_TEMPLATE_PACK = 'bootstrap4'

# STATIC_ROOT = os.path.join(BASE_DIR, 'assets')

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

LOGIN_REDIRECT_URL = 'blogHome'
LOGIN_URL = 'login'

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

当前 django 项目的requirements.txt

requirements.txt

boto3==1.9.96
botocore==1.12.96
certifi==2018.10.15
chardet==3.0.4
dj-database-url==0.5.0
Django==2.1
django-crispy-forms==1.7.2
django-heroku==0.3.1
django-storages==1.7.1
docutils==0.14
gunicorn==19.9.0
idna==2.7
jmespath==0.9.3
Pillow==5.2.0
python-dateutil==2.8.0
pytz==2018.5
requests==2.19.1
s3transfer==0.2.0
six==1.12.0
urllib3==1.23
whitenoise==4.1.2

heroku 日志

 ›   Warning: heroku update available from 7.52.0 to 7.54.0.
2021-06-12T09:49:28.101517+00:00 app[web.1]: Arbiter(self).run()
2021-06-12T09:49:28.101521+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 232, in run
2021-06-12T09:49:28.101745+00:00 app[web.1]: self.halt(reason=inst.reason, exit_status=inst.exit_status)
2021-06-12T09:49:28.101747+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 345, in halt
2021-06-12T09:49:28.101999+00:00 app[web.1]: self.stop()
2021-06-12T09:49:28.102004+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 393, in stop
2021-06-12T09:49:28.102241+00:00 app[web.1]: time.sleep(0.1)
2021-06-12T09:49:28.102246+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 245, in handle_chld
2021-06-12T09:49:28.102398+00:00 app[web.1]: self.reap_workers()
2021-06-12T09:49:28.102404+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 525, in reap_workers
2021-06-12T09:49:28.102716+00:00 app[web.1]: raise HaltServer(reason, self.WORKER_BOOT_ERROR)
2021-06-12T09:49:28.102754+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
2021-06-12T09:49:28.205668+00:00 heroku[web.1]: Process exited with status 1
2021-06-12T09:49:28.284579+00:00 heroku[web.1]: State changed from starting to crashed
2021-06-12T09:49:34.912538+00:00 app[api]: Release v9 created by user dhruv.singhal2612@gmail.com
2021-06-12T09:49:34.912538+00:00 app[api]: Set DEBUG_VALUE config vars by user dhruv.singhal2612@gmail.com
2021-06-12T09:49:35.143657+00:00 heroku[web.1]: State changed from crashed to starting
2021-06-12T09:49:41.619208+00:00 heroku[web.1]: Starting process with command `gunicorn django_project.wsgi`
2021-06-12T09:49:45.299640+00:00 app[web.1]: [2021-06-12 09:49:45 +0000] [4] [INFO] Starting gunicorn 19.9.0
2021-06-12T09:49:45.300103+00:00 app[web.1]: [2021-06-12 09:49:45 +0000] [4] [INFO] Listening at: http://0.0.0.0:16332 (4)
2021-06-12T09:49:45.300346+00:00 app[web.1]: [2021-06-12 09:49:45 +0000] [4] [INFO] Using worker: sync
2021-06-12T09:49:45.302082+00:00 app[web.1]: /app/.heroku/python/lib/python3.9/os.py:1023: RuntimeWarning: line buffering (buffering=1) isn't supported in binary mode, the default buffer size will be used
2021-06-12T09:49:45.302083+00:00 app[web.1]: return io.open(fd, *args, **kwargs)
2021-06-12T09:49:45.306106+00:00 app[web.1]: [2021-06-12 09:49:45 +0000] [7] [INFO] Booting worker with pid: 7
2021-06-12T09:49:45.312314+00:00 app[web.1]: [2021-06-12 09:49:45 +0000] [7] [ERROR] Exception in worker process
2021-06-12T09:49:45.312315+00:00 app[web.1]: Traceback (most recent call last):
2021-06-12T09:49:45.312326+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2021-06-12T09:49:45.312326+00:00 app[web.1]: worker.init_process()
2021-06-12T09:49:45.312327+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/workers/base.py", line 129, in init_process
2021-06-12T09:49:45.312328+00:00 app[web.1]: self.load_wsgi()
2021-06-12T09:49:45.312328+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
2021-06-12T09:49:45.312328+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2021-06-12T09:49:45.312329+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi
2021-06-12T09:49:45.312329+00:00 app[web.1]: self.callable = self.load()
2021-06-12T09:49:45.312329+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
2021-06-12T09:49:45.312330+00:00 app[web.1]: return self.load_wsgiapp()
2021-06-12T09:49:45.312330+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
2021-06-12T09:49:45.312331+00:00 app[web.1]: return util.import_app(self.app_uri)
2021-06-12T09:49:45.312331+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/util.py", line 350, in import_app
2021-06-12T09:49:45.312331+00:00 app[web.1]: __import__(module)
2021-06-12T09:49:45.312332+00:00 app[web.1]: ModuleNotFoundError: No module named 'django_project'
2021-06-12T09:49:45.312475+00:00 app[web.1]: [2021-06-12 09:49:45 +0000] [7] [INFO] Worker exiting (pid: 7)
2021-06-12T09:49:45.342275+00:00 app[web.1]: [2021-06-12 09:49:45 +0000] [8] [INFO] Booting worker with pid: 8
2021-06-12T09:49:45.354724+00:00 app[web.1]: [2021-06-12 09:49:45 +0000] [8] [ERROR] Exception in worker process
2021-06-12T09:49:45.354725+00:00 app[web.1]: Traceback (most recent call last):
2021-06-12T09:49:45.354726+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2021-06-12T09:49:45.354726+00:00 app[web.1]: worker.init_process()
2021-06-12T09:49:45.354727+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/workers/base.py", line 129, in init_process
2021-06-12T09:49:45.354727+00:00 app[web.1]: self.load_wsgi()
2021-06-12T09:49:45.354728+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
2021-06-12T09:49:45.354728+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2021-06-12T09:49:45.354728+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi
2021-06-12T09:49:45.354729+00:00 app[web.1]: self.callable = self.load()
2021-06-12T09:49:45.354729+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
2021-06-12T09:49:45.354730+00:00 app[web.1]: return self.load_wsgiapp()
2021-06-12T09:49:45.354730+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
2021-06-12T09:49:45.354730+00:00 app[web.1]: return util.import_app(self.app_uri)
2021-06-12T09:49:45.354731+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/util.py", line 350, in import_app
2021-06-12T09:49:45.354731+00:00 app[web.1]: __import__(module)
2021-06-12T09:49:45.354732+00:00 app[web.1]: ModuleNotFoundError: No module named 'django_project'
2021-06-12T09:49:45.354973+00:00 app[web.1]: [2021-06-12 09:49:45 +0000] [8] [INFO] Worker exiting (pid: 8)
2021-06-12T09:49:45.396511+00:00 app[web.1]: Traceback (most recent call last):
2021-06-12T09:49:45.396519+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 203, in run
2021-06-12T09:49:45.396872+00:00 app[web.1]: self.manage_workers()
2021-06-12T09:49:45.396873+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 545, in manage_workers
2021-06-12T09:49:45.397354+00:00 app[web.1]: self.spawn_workers()
2021-06-12T09:49:45.397381+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 617, in spawn_workers
2021-06-12T09:49:45.397935+00:00 app[web.1]: time.sleep(0.1 * random.random())
2021-06-12T09:49:45.397962+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 245, in handle_chld
2021-06-12T09:49:45.398390+00:00 app[web.1]: self.reap_workers()
2021-06-12T09:49:45.398463+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 525, in reap_workers
2021-06-12T09:49:45.398958+00:00 app[web.1]: raise HaltServer(reason, self.WORKER_BOOT_ERROR)
2021-06-12T09:49:45.399217+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
2021-06-12T09:49:45.399240+00:00 app[web.1]: 
2021-06-12T09:49:45.399240+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2021-06-12T09:49:45.399241+00:00 app[web.1]: 
2021-06-12T09:49:45.399265+00:00 app[web.1]: Traceback (most recent call last):
2021-06-12T09:49:45.399340+00:00 app[web.1]: File "/app/.heroku/python/bin/gunicorn", line 8, in <module>
2021-06-12T09:49:45.399650+00:00 app[web.1]: sys.exit(run())
2021-06-12T09:49:45.399677+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 61, in run
2021-06-12T09:49:45.400017+00:00 app[web.1]: WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
2021-06-12T09:49:45.400048+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/base.py", line 223, in run
2021-06-12T09:49:45.400760+00:00 app[web.1]: super(Application, self).run()
2021-06-12T09:49:45.400788+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/base.py", line 72, in run
2021-06-12T09:49:45.401131+00:00 app[web.1]: Arbiter(self).run()
2021-06-12T09:49:45.401164+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 232, in run
2021-06-12T09:49:45.401541+00:00 app[web.1]: self.halt(reason=inst.reason, exit_status=inst.exit_status)
2021-06-12T09:49:45.401569+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 345, in halt
2021-06-12T09:49:45.402002+00:00 app[web.1]: self.stop()
2021-06-12T09:49:45.402029+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 393, in stop
2021-06-12T09:49:45.402490+00:00 app[web.1]: time.sleep(0.1)
2021-06-12T09:49:45.402519+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 245, in handle_chld
2021-06-12T09:49:45.403126+00:00 app[web.1]: self.reap_workers()
2021-06-12T09:49:45.403154+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 525, in reap_workers
2021-06-12T09:49:45.403652+00:00 app[web.1]: raise HaltServer(reason, self.WORKER_BOOT_ERROR)
2021-06-12T09:49:45.403847+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
2021-06-12T09:49:45.504630+00:00 heroku[web.1]: Process exited with status 1
2021-06-12T09:49:45.573000+00:00 heroku[web.1]: State changed from starting to crashed
2021-06-12T09:50:28.064356+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=djangoblog-project.herokuapp.com request_id=55fab425-ef64-485a-892c-cbf2120401ce fwd="223.184.234.62" dyno= connect= service= status=503 bytes= protocol=https
2021-06-12T09:50:28.695891+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=djangoblog-project.herokuapp.com request_id=2c5d885e-39a1-4683-aa17-28efabadd898 fwd="223.184.234.62" dyno= connect= service= status=503 bytes= protocol=https

2 个答案:

答案 0 :(得分:0)

如果您使用的是 django-heroku 包,那么您必须将其添加到您的 settings.py

将以下导入语句添加到 settings.py 的顶部:

import django_heroku

然后将以下内容添加到 settings.py 的底部:

# Activate Django-Heroku.
django_heroku.settings(locals())

查看官方文档。 https://devcenter.heroku.com/articles/django-app-configuration#settings-py-changes

答案 1 :(得分:0)

您是否在项目文件夹中创建了 Procfile 文件? 如果您使用的是 Windows PS,您可以输入 terminal:

echo web: gunicorn <<YOUR_PROJECT>>.wsgi --log-file - > Procfile

另一种可能的解决方案是使用 Postgres 而不是 MySQL:

settings.py中:

DATABASES = {
    'default': dj_database_url.config()
}

requirements.txt中:

psycopg2-binary==2.8.6