LoginSignup
0
0

More than 1 year has passed since last update.

DjangoでHerokuデプロイするための環境構築とエラー

Posted at

#はじめに
以前anaconda仮想環境下でDjangoのWEBアプリを作成した後、Herokuでデプロイができず、半年ほどエラーに悩まされましたw
私が遭遇したエラーとその解決策を記します。
同じエラーになっている人の助けになれば幸いです。

###エラー(1)
ruquirements.txtがやたら多い

###解決策
仮想環境構築しなおす(後で詳しく方法があるので見てください)

###エラー(2)
pys…
###解決策
postgreSQLをインストール
###エラー(3)
pythonのパスが通ってない
###解決策
pythonのパスを通せ

#今からやること
他にもエラーがあったかもしれませんが、覚えているのは上記3つです。
これからHerokuでデプロイできる仮想環境を構築する方法を記します。
Herokuでデプロイする方法は以下の記事をご覧ください。
他の方のですが、こちらの記事に詳しく書いてあります。

環境

・windows
・Git Bash
・python 3.
・Django
Herokuでデプロイする際にgithubが必要なのでGit Bashをインストールしてください。ダウンロードはこちら
この記事ではほぼすべてコマンドはGit Bashで打ってます。

環境構築

terminal
$ mkdir heroku_deploy        //ディレクトリを作成
$ cd heroku_deploy
$ python -m venv env         //仮想環境を構築

仮想環境の有効化・無効化

envフォルダがある所で仮想環境の有効化を行う

termanal
$ ls                               //ディレクトリ内にあるファイルの一覧を表示

windowsでgit bashを使う場合

terminal
$ source ./env/Scripts/activate  //仮想環境の有効化
$ deactivate             //仮想環境の無効化

windowsでコマンドプロンプトを使う場合

terminal
$ env\Scripts\activate           //仮想環境の有効化
$ deactivate             //仮想環境の無効化

#インストール

terminal
$ pip install --upgrade pip 
$ pip install --upgrade setuptools
$ pip install django
$ pip list

Djangoのプロジェクトを作成

terminal
$ django-admin startproject web_test
$ cd web_test
$ python manage.py migrate
$ python manage.py runserver   //WEBサーバー起動

ブラウザでhttp://localhost:8000/hello
にアクセス

Djangoのアプリケーションを作成

terminal
($ cd web_test)
$ python manage.py startapp hello
現在の状況
heroku_deploy 
|-----env(仮想環境、触らない)
|-----web_test
	|------hello
	|------web_test(settings.pyなど)
web_test/settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hello', # 追加
]

#html,CSSを作成

heroku_deploy 
|-----env(仮想環境、触らない)
|-----web_test
	|------hello(htmlとかcssとか)  
			|-------templates            #追加
				|--------hello           #追加
					|-------index.html   #追加
	|------web_test(設定ファイルなど
templates/hello/index.html
<!doctype html>
<html lang="ja">
<head>
    <meta charset="utf-8">
    <title>hello</title>
</head>
<body>
    <h1>こんにちは</h1>
    <p>サンプルページです。</p>
</body>
</html>
web_test/web_test/urls.py
from django.contrib import admin
from django.urls import path
import hello.views as hello

urlpatterns = [
    path('admin/',admin.site.urls),
    path('hello/',hello.index)
]
web_test/hello/views.py
from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.

def index(request):
    return render(request, 'hello/index.html')

WEBサーバーを起動してちゃんと動いているか確認します。

terminal
$ python manage.py runserver

ブラウザでhttp://localhost:8000/hello
にアクセス

###CSSを反映する

静的ファイル(画像ファイル、cssなど)は「static」フォルダに入れる。

heroku_deploy
|-----env(仮想環境、触らない)
|-----web_test
	|------hello(htmlとかcssとか)
			|-------static               #追加
				|--------hello           #追加
					|-------style.css    #追加
	|------web_test(設定ファイルなど
style.css
body{
    color:gray;
    font-size:16pt;
   }
   
   h1{
    color:blue;
    opacity:0.2;
    font-size:60pt;
    margin-top:-20pt;
    margin-bottom:0px;
   }
   
   p{
    margin:10px;
   }
   
   a{
    color:blue;
    text-decoration:none;
   }

index.htmlを修正

templates/hello/index.html
{% load static %}
<!doctype html>
<html lang="ja">
<head>
    <meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="{% static 'hello/css/style.css' %}" />
    <title>hello</title>
</head>
<body>
    <h1>こんにちは</h1>
    <p>CSS反映した</p>
</body>
</html>

WEBサーバーを起動してちゃんと動いているか確認します。

terminal
$ python manage.py runserver

ブラウザでhttp://localhost:8000/hello
にアクセス

#Herokuでデプロイ

他の方のですが、こちらの記事に詳しく書いてあります。

丸投げではない

0
0
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
0
0