0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Django Gitアップ前のシークレット情報設定

Last updated at Posted at 2021-11-23

#はじめに
Gitへアップする前のシークレット情報の設定方法を手順化しました。

Pythonのバージョン:3.8.2
djangoのバージョン:3.2.9
gitのバージョン:2.30.2

#目次
1.django-environのインストール
2.「.env」の作成
3.settings.pyの修正
4.「.gitignore」の確認

##インストール
django-environをインストールします。

pip install django-environ

##フォルダとファイルの作成
BASE_DIR(manage.pyがあるディレクトリ)にsecretsフォルダを作成します。
作成後、secretsフォルダ内に「.env」を作成します。
.envにproject/settings.pyの下記の情報を記載します。

・SECRET_KEY
・DEBUG
・ALLOWED_HOSTS

secrets/.env
SECRET_KEY=secret_key #ここにsecret_keyを貼り付ける ※ダブルクォーテーションは取り除く
DEBUG=True
ALLOWED_HOSTS=*
DATABASE_URL=sqlite:///db.sqlite3

##settings.pyの修正
projectのsettings.pyの中を修正していく

project/settings.py
#追記---
import environ

env = environ.Env()
root = environ.Path(BASE_DIR / "secrets")
env.read_env(root(".env"))

#修正---
SECRET_KEY = env.str("SECRET_KEY")
DEBUG = env.bool("DEBUG")
ALLOWED_HOSTS = env.list("ALLOWED_HOSTS")

DATABASES = {
    'default':env.db(),
}

##.gitignoreの確認
.gitignoreに.envの記載があることを確認
ない場合は追記

.gitignore
# Environments
.env

設定は以上

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?