我想把Django应用程序上传到Heroku

您好,非常感谢您一直以来的照顾。

【环境】
Surface Laptop2
Windows 1809
Python 3.7.4(在命令行中执行)
Django 2.2.6

【目标实现】
希望通过使用Heroku将使用Django构建的应用程序发布。

【状况】
在Heroku上使用Django应用程序部署不成功,构建过程也失败了。

【进行的步骤】(参考链接等)
(1)参考了以下两个网站:
https://qiita.com/frosty/items/66f5dff8fc723387108c
https://qiita.com/RyuSA/items/0cbc7d5b0145585861a8
将settings.py进行了修改,修改如下:
from socket import gethostname # 通过这个可以获取主机名***A
import django_heroku
import os
import dj_database_url

hostname = gethostname() # 将从A获取的主机名赋值给hostname

基本目录= os.path.dirname(os.path.dirname(os.path.abspath(file)))

安装的应用程序 = [
‘polls.apps.PollsConfig’,
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
]

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’,
‘whitenoise.middleware.WhiteNoiseMiddleware’, # Added this line
]

中间件 = [
‘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’,
‘whitenoise.middleware.WhiteNoiseMiddleware’, # 添加了这行
]

ROOT_URLCONF = ‘mysite.urls’ -> 网站根目录的URL配置为’mysite.urls’

模板 = [
{
‘后端’: ‘django.template.backends.django.DjangoTemplates’,
‘目录’: [os.path.join(BASE_DIR, ‘templates’)],
‘应用目录’: True,
‘选项’: {
‘上下文处理器’: [
‘django.template.context_processors.debug’,
‘django.template.context_processors.request’,
‘django.contrib.auth.context_processors.auth’,
‘django.contrib.messages.context_processors.messages’,
],
},
},
]

WSGI_APPLICATION = ‘mysite.wsgi.application’ 可以转述为 “WSGI_APPLICATION = ‘mysite.wsgi.application'”

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’,
},
]

身份验证_密码验证 = [
{
‘名称’: ‘django.contrib.auth.password_validation.UserAttributeSimilarityValidator’,
},
{
‘名称’: ‘django.contrib.auth.password_validation.MinimumLengthValidator’,
},
{
‘名称’: ‘django.contrib.auth.password_validation.CommonPasswordValidator’,
},
{
‘名称’: ‘django.contrib.auth.password_validation.NumericPasswordValidator’,
},
]

LANGUAGE_CODE = ‘zh-cn’
TIME_ZONE = ‘UTC’
USE_I18N = True
USE_L10N = True
USE_TZ = True

调试 = False
如果不是调试:
秘钥 = os.environ[‘秘钥’]

DEBUG = 假

尝试:
尝试导入 from .local_settings import * # 导入 local_settings 模块的全部内容
如果导入失败,则跳过。

STATIC_URL = ‘/static/’
STATIC_ROOT = ‘staticfiles’
STATIC_ROOT = os.path.join(BASE_DIR, ‘staticfiles’)

静态网址= ‘/static/’
静态根目录 = ‘staticfiles’
静态根目录 = os.path.join(BASE_DIR, ‘staticfiles’)

如果主机名中存在”DESKTOP-877IM6E”,则:
# 调试环境
# DEBUG = True
DATABASES = {
# 当django没有选择其他数据库时,默认使用别名为default的数据库
‘default’: {
‘ENGINE’: ‘django.db.backends.sqlite3’,
‘NAME’: os.path.join(BASE_DIR, ‘db.sqlite3’),
}
}
ALLOWED_HOSTS = [‘*’] # 因为是本地开发环境,所以不需要域名

否则:
DEBUG = False
db_from_env = dj_database_url.config()
DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.postgresql_psycopg2’,
‘NAME’: ‘name’,
‘USER’: ‘user’,
‘PASSWORD’: ”,
‘HOST’: ‘host’,
‘PORT’: ”,
}
}
ALLOWED_HOSTS = [‘https://salty-beyond-49092.herokuapp.com/’]

详细设置

a.png

成果、結局、結果、結議

b.png

不明白的事情等等

c.png

请多多关照。

广告
将在 10 秒后关闭
bannerAds