1
1

More than 3 years have passed since last update.

Djangoアプリをherokuにアップロードしたい

Posted at

お世話になります。

【環境】
Surface Laptop2
Windows 1809
Python 3.7.4 (コマンドラインで実行してるやつ)
Django 2.2.6

【実現したいこと】
Djangoで作ったアプリをherokuを使って公開したい

【状況】
HerokuにDjangoアプリでデプロイがうまくいかなく、ビルドが成功しません。

【行った手順】(参考にしたURLなど)
(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に代入

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file)))

INSTALLED_APPS = [
'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', # ←を追加
]

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'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 = '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',
},
]

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

DEBUG = False
if not DEBUG:
SECRET_KEY = os.environ['SECRET_KEY']

DEBUG = False

try:
from .local_settings import * # loca_settings モジュールのすべてをインポートしている 
except ImportError:
pass

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

if "DESKTOP-877IM6E" in hostname:
# デバッグ環境
# DEBUG = True
DATABASES = {
# djangoは他のデータベースが選択されていない時に、defaultの別名で
# データベースを使う
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
ALLOWED_HOSTS = ['*'] # ローカル開発環境なので、ドメインはなくてよいから
else:
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/']

詳細設定

(2)以下の画像のように、ファイルが設定されており、requirements.txtやProcfileは設定してあります。
また、画像で黒文字に覆われているものは、自分のPC名です。
a.png

結果

そして、herokuでデプロイして、指定されたURLをブラウザで表示してみると以下の様になります
b.png

【わからないことなど】

Django初心者で、なぜBad requestが出るのか、全くわかりません。
エラーも出ていないので、いまいちどこをどう直せばいいのかわかんないです。
デベロッパーツールのエラーメッセージには、
「Failed to load resource: the server responded with a status of 400 (Bad Request)」
のようなエラーメッセージが表示されています。
以下の画像の様に、heroku statusで見ても
データベースなどに問題はないそうです、
c.png

よろしくお願いします。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1