Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
本番運用を想定したDjango settings.pyの書き方入門(OSC Fall 2021...
Search
Ryuji Tsutsui
October 23, 2021
Technology
1
1.1k
本番運用を想定したDjango settings.pyの書き方入門(OSC Fall 2021 version)
オープンソースカンファレンス2021 Fall
資料
Ryuji Tsutsui
October 23, 2021
Tweet
Share
More Decks by Ryuji Tsutsui
See All by Ryuji Tsutsui
本番運用を想定したDjango settings.pyの書き方入門
ryu22e
1
2.3k
os.makedirsの細かすぎるセキュリティの話
ryu22e
0
50
HashiCorpの新ツール 「Waypoint」をCloud Runで使ってみた
ryu22e
0
400
Pythonチュートリアルイベント「Python Boot Camp」の紹介
ryu22e
0
170
Pythonとコミュニティと私
ryu22e
0
100
Other Decks in Technology
See All in Technology
UI State設計とテスト方針
rmakiyama
4
930
Oracle Cloud Infrastructure:2024年12月度サービス・アップデート
oracle4engineer
PRO
1
500
The key to VCP-VCF
mirie_sd
0
140
20240522 - 躍遷創作理念 @ PicCollage Workshop
dpys
0
290
2024年にチャレンジしたことを振り返るぞ
mitchan
0
170
OPENLOGI Company Profile
hr01
0
57k
20241218_マルチアカウント環境におけるIAM_Access_Analyzerによる権限管理.pdf
nrinetcom
PRO
3
140
開発生産性向上! 育成を「改善」と捉えるエンジニア育成戦略
shoota
2
820
OPENLOGI Company Profile for engineer
hr01
1
17k
Alignment and Autonomy in Cybozu - 300人の開発組織でアラインメントと自律性を両立させるアジャイルな組織運営 / RSGT2025
ama_ch
1
860
生成AIによるテスト設計支援プロセスの構築とプロセス内のボトルネック解消の取り組み / 20241220 Suguru Ishii
shift_evolve
0
160
効率的な技術組織が作れる!書籍『チームトポロジー』要点まとめ
iwamot
2
180
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
222
9k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
The Invisible Side of Design
smashingmag
299
50k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2k
The World Runs on Bad Software
bkeepers
PRO
66
11k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
Building an army of robots
kneath
302
44k
4 Signs Your Business is Dying
shpigford
182
21k
Transcript
ຊ൪ӡ༻Λఆͨ͠ Django settings.pyͷॻ͖ ํೖ Ryuji Tsutsui Open Source Conference 2021
Online/Fallࢿྉ 2021/10/23
ࣗݾհ • Ryuji Tsutsui@ryu22e • Ұൠࣾஂ๏ਓPyCon JP Association͔Βདྷ·ͨ͠ • ීஈຊͷϑΟϯςοΫܥاۀͰDjangoΛͬͯWebαʔϏε
Λ࡞͍ͬͯ·͢ • Djangoྺ8͙Β͍ • Python Boot CampίΞελοϑͱͯ͠׆ಈ͍ͯ͠·͢
ຊͷએ ٕज़ධࣾץʰPython࣮ફϨγϐʱΛࣥචத 20221݄ൃച༧ఆ ϖʔδ: ະఆ ֹۚ: ະఆ ʮPythonϥΠϒϥϦݫબϨγϐʯΛେ෯ʹՃ චɺվగ!!!
ࠓ͢͜ͱ • ຊ൪ڥͰ͑ΔDjangoΞϓϦέʔγϣϯͷઃܭʹ͍ͭͯɺ Έͳ͞ΜͱϊϋΛز͔ͭڞ༗͠·͢ • ࠓճsettings.pyʢDjangoͷઃఆϑΝΠϧʣͷॻ͖ํʹ͍ͭͯ͠ ·͢
ओʹ3ʹ͍ͭͯ͠·͢ • 1. ֤ڥ༻ͷઃఆ • 2. ൿີͷͷॻ͖ํ • 3. ϩάઃఆ
͜Μͳਓ͚ͷηογϣϯͰ͢ • ʮDjangoͷνϡʔτϦΞϧ͙Β͍ͬͨ͜ͱ͕͋Δ͚Ͳຊ൪ڥ Ͱಈ͔͢ΞϓϦέʔγϣϯΛ࡞ͬͨ͜ͱͳ͍ʯͱ͍͏ਓ͚Ͱ͢
ʢ·͓ͣ͞Β͍ʣ settings.pyͬͯԿ͚ͩͬʁ
settings.pyͱʁ $ django-admin startproject django_example $ cd django_example $ tree
django_example django_example !"" __init__.py !"" asgi.py !"" settings.py !"" urls.py #"" wsgi.py %KBOHPϓϩδΣΫτશମͰࢀরͰ͖Δάϩʔόϧͳ ఆΛఆٛͰ͖Δ
settings.pyͷॻ͖ํ DEBUG = True LANGUAGE_CODE = 'ja' TIME_ZONE = 'UTC'
ී௨ʹ1ZUIPOͷఆΛॻ͚ͩ͘
settings.pyʹॻ͍ͨઃఆΛݺͼग़͢ํ๏ >>> from django.conf import settings >>> settings.DEBUG True >>>
settings.LANGUAGE_CODE 'ja' EKBOHPDPOGTFUUJOHTΛܦ༝ͯ͠ݺͼ·͢
1. ֤ڥ༻ͷઃఆ
͜Μͳ࣌Ͳ͏͢Ε͍͍ͷͩΖ͏…ʁ INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles',
'debug_toolbar', # ։ൃڥ͔͠Θͳ͍ઃఆ ] ALLOWED_HOSTS = [ 'example.com', # ຊ൪ڥ͔͠Θͳ͍ઃఆ ] ։ൃڥɾຊ൪ڥͲͪΒʹͱͬͯෆཁͳઃఆ ؚ͕·Ε͍ͯΔʜ
ΞϓϦىಈલʹෆཁͳߦΛίϝϯτΞτ͢Δʁ INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles',
# 'debug_toolbar', # ։ൃڥ͔͠Θͳ͍ઃఆ ] ALLOWED_HOSTS = [ 'example.com', # ຊ൪ڥ͔͠Θͳ͍ઃఆ ] ϛε͕͋Γͦ͏ͳͷͰΓͨ͘ͳ͍
༗ޮແޮΛΓସ͑ΔΦϓγϣϯΛ༻ҙ͢Δʁ MODE = ... # ڥม͔MODEΛఆٛͨ͠ผͷϑΝΠϧΛಡΈࠐΉ INSTALLED_APPS = [ 'django.contrib.admin',
'django.contrib.auth', # ʢলུʣ ] if MODE == 'local': INSTALLED_APPS += ['debug_toolbar'] ίϝϯτΞτΑΓΦϖϛεͷՄ ೳੑͦ͏͚ͩͲʜ
༗ޮແޮΛΓସ͑ΔΦϓγϣϯΛ༻ҙ͢Δʁ if MODE == 'local': INSTALLED_APPS.append('debug_toolbar') if MODE == 'production':
ALLOWED_HOSTS = ['example.com'] elif MODE == 'staging': ALLOWED_HOSTS = ['staging.example.com'] else: ALLOWED_HOSTS = ['*'] if MODE == 'staging': ... Φϓγϣϯ͕૿͑Δͱઃఆ͕ෳࡶԽ ͦ͠͏
͜Μͳ࣌ઃఆϑΝΠϧΛڥ͝ͱʹ͚· ͠ΐ͏
͜ͷΑ͏ͳߏ͕͓קΊ django_example #"" settings # settings.py͕͋ͬͨॴʹ͜ΕΛ࡞Δ !"" __init__.py !"" base.py
# ͯ͢ͷڥͰ͏ڞ௨ͷઃఆ !"" local.py # ։ൃڥ༻ͷઃఆ !"" production.py # ຊ൪ڥ༻ͷઃఆ #"" staging.py # εςʔδϯάڥ༻ͷઃఆ TFUUJOHTQZফ͢
֤ϑΝΠϧͷ༰ INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', # ʢলུʣ ] ROOT_URLCONF
= 'django_example.urls' WSGI_APPLICATION = 'django_example.wsgi.application' base.pyʢڞ௨ͷઃఆʣ
from .base import * DEBUG = False ALLOWED_HOSTS = [
'staging.example.com', ] ֤ϑΝΠϧͷ༰ from .base import * DEBUG = False ALLOWED_HOSTS = [ 'example.com', ] from .base import * DEBUG = True INSTALLED_APPS += ['debug_toolbar'] stagings.pyʢεςʔδϯάڥ༻ʣ local.pyʢ։ൃڥ༻ʣ production.pyʢຊ൪ڥ༻ʣ ͜͜Ͱڞ௨ͷઃఆ ΛಡΈࠐΉ
Ͳ͏ͬͯ͏ઃఆϑΝΠϧΛΓସ͑Δ͔ • ڥมDJANGO_SETTINGS_MODULEΛ͍·͠ΐ͏ • ྫ͑ɺຊ൪ڥͳΒ͜͏͠·͢ • DJANGO_SETTINGS_MODULE="django_example.settings.production" • ϑΝΠϧύεͰͳͯ͘Pythonͷimportจʹॻ͘ͱ͖ͷυοτͰܨ͛ͨॻ͖ ํͰ͋Δ͜ͱʹҙ͍ͯͩ͘͠͞
• Ұ࣌తʹ͏ઃఆϑΝΠϧΛΓସ͍͑ͨͳΒɺDjangoίϚϯυʹ--settings ΦϓγϣϯΛࢦఆ͠·͢
ڥมΛͲ͜ʹઃఆ͢Ε͍͍͔ʁ • ͍ͬͯΔϓϥοτϑΥʔϜʢΦϯϓϨ or ΫϥυʣʹΑͬͯҧ͏ ͷͰɺҰ֓ʹ͜Εɺͱ͍͏ͷݴ͑·ͤΜ͕ɺز͔ͭྫΛڍ͛·͢
ڥมͷઃఆํ๏ྫ • .bashrcʹʮexport DJANGO_SETTINGS_MODULE="..."ʯΛॻ͍͓ͯ ͘ • systemdͷEnvironmentFileΛར༻͢Δ • AWS Systems
Manager ύϥϝʔλετΞΛར༻͢Δ • Google Cloud Secret ManagerΛར༻͢Δ
manage.pyͷฤू͕ඞཁ #!/usr/bin/env python """Django's command-line utility for administrative tasks.""" import
os import sys def main(): """Run administrative tasks.""" os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_example.settings') # ʢলུʣ ͜͜ΛʮEKBOHP@FYBNQMFTFUUJOHTMPDBMʯʹมߋ
2. ൿີͷͷॻ͖ํ
͜ΜͳઃఆΛॻ͍͍͚ͯͳ͍ʂ
ͪͳΈʹGitHubͰࠓͷίʔυΛެ։͢Δͱ͜Μͳϝʔϧ͕ಧ͖·͢
ൿີͷڥม͔ΒಡΈऔΔΑ͏ʹ import os AWS_ACCESS_KEY_ID = os.environ.get( "DJANGO_AWS_ACCESS_KEY_ID" ) ͨͩɺ͜ͷॻ͖ํͩͱڥมͷઃఆ࿙Εͷࡍʹ /POF͕ೖͬͯ͠·͏ͷͰɺ͏ͻͱඞཁ
ඞਢͷڥมͲ͏ͬͯॻ͘ʁ import os from django.core.exception import ImproperlyConfigured # os.environʹ֘͢Δ͕ͳ͚Εྫ֎Λૹग़͢ΔؔΛ࡞Δ def
get_env_variable(var_name): try: os.environ[var_name] except KeyError: # os.environʹ֘͢ΔΩʔ͕ͳ͍߹ʹԿͷΩʔ͕Γͳ͍͔Λྫ֎ϝοηʔδʹؚΉ error_msg = "Set the {} environment variable".format(var_name) raise ImproperlyConfigured(error_msg) Ҿ༻ݩʮ5XP4DPPQTPG%KBOHPYʯ ϖʔδ
͜ΜͳϥΠϒϥϦ͓קΊ • django-environ • PyPI: https://pypi.org/project/django-environ/ • EnvΫϥεܦ༝ͰڥมΛݺͼग़͢ͱɺઃఆ࿙ΕΛݕग़Ͱ͖· ͢ɻ •
bool, intͳͲܕมͬͯ͘Ε·͢ɻ
3. ϩάઃఆ
DjangoͷϩάઃఆͷΓํ • LOGGINGͱ͍͏߲ʹࣙॻܕͰઃఆΛॻ͖·͢ • ʢͬ͘͟Γઆ໌͢ΔͱʣϩάΛͲ͜ʹग़ྗ͢Δ͔Λ͜͜ͰܾΊ·͢ • ग़ྗઌͷྫ • ϝʔϧ •
ඪ४ग़ྗ • ϩʔΧϧϑΝΠϧ • ϩάϩʔςʔγϣϯʢݹ͍ϩάͷΞʔΧΠϒআʣͰ͖·͢ • ʮϩάϨϕϧʯʢDEBUG, INFO, WARNING, ERROR, CRITICALʣϩάग़ྗݩͷϞδϡʔϧʹ Αͬͯग़ྗઌΛΓସ͑Δ͜ͱͰ͖·͢
LOGGINGͷઃఆ্ɺग़ྗઌඪ४ग़ྗͷΈʹ ͢Δ • LOGGING্Ͱͷग़ྗઌͯ͢ඪ४ग़ྗ • ඪ४ग़ྗͷϩάΛͲ͏ѻ͏͔ɺΞϓϦͷ֎ʹ͋ΔϛυϧΣΞʹ ͤ·͠ΐ͏ • Fluentd •
Amazon CloudWatch Logs • Google Cloud Logging
cookiecutter-django͓קΊ • cookiecutterςϯϓϨʔτΛݩʹϓϩδΣΫτΛ࡞͢Δπʔϧ • URL: https://github.com/pydanny/cookiecutter-django • LOGGINGͷઃఆྫ͗͢ΔͷͰ͜ͷεϥΠυʹࡌͤ·ͤΜͰ͠ ͕ͨɺ͜ͷςϯϓϨʔτͰ࡞ͬͨϓϩδΣΫτʹॻ͍͍ͯΔ༰Λಡ ΜͰ͍ͩ͘͞
cookiecutter-djangoͷ͍ํ $ pip install cookiecutter # macOSͳΒbrew install cookiecutter Մ
$ cookiecutter https://github.com/pydanny/cookiecutter-django $ # ʢ৭ʑ࣭͞Ε·͕͢ɺͱΓ͋͑ͣͬͯΈΔ͚ͩͳΒશ෦EnterΛԡͯ͠OKʣ $ cd my_awesome_project $ ls config/settings __init__.py base.py local.py production.py test.py
·ͱΊ • settings.pyڥ͝ͱʹϑΝΠϧΛ͚ͯɺڥม DJANGO_SETTINGS_MODULEͰಡΉϑΝΠϧΛΓସ͑·͠ΐ͏ • ൿີͷΛsettings.pyʹʹॻ͘ͷઈରμϝɻڥม͔Βಡ·ͤ·͠ΐ ͏ɻdjango-environ͓קΊ • ϩάग़ྗγϯϓϧʹඪ४ग़ྗͷΈɻอଘઌΞϓϦέʔγϣϯͷ֎ʹ͋Δϛ υϧΣΞʹཧͤ͞·͠ΐ͏
• Ҏ্3ΛॳΊ͔ΒΈࠐΜͩςϯϓϨʔτcookiecutter-django͓קΊͰ͢
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝· ͨ͠