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.3k
本番運用を想定した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.5k
os.makedirsの細かすぎるセキュリティの話
ryu22e
0
53
HashiCorpの新ツール 「Waypoint」をCloud Runで使ってみた
ryu22e
0
430
Pythonチュートリアルイベント「Python Boot Camp」の紹介
ryu22e
0
180
Pythonとコミュニティと私
ryu22e
0
110
Other Decks in Technology
See All in Technology
Go で言うところのアレは TypeScript で言うとコレ / Kyoto.なんか #7
susisu
2
700
EKS Pod Identity における推移的な session tags
z63d
1
200
ECS モニタリング手法大整理
yendoooo
1
120
[CV勉強会@関東 CVPR2025 読み会] MegaSaM: Accurate, Fast, and Robust Structure and Motion from Casual Dynamic Videos (Li+, CVPR2025)
abemii
0
180
現場が抱える様々な問題は “組織設計上” の問題によって生じていることがある / Team-oriented Organization Design 20250827
mtx2s
2
710
Devinを使ったモバイルアプリ開発 / Mobile app development with Devin
yanzm
0
170
会社にデータエンジニアがいることでできるようになること
10xinc
9
1.5k
Android Studio の 新しいAI機能を試してみよう / Try out the new AI features in Android Studio
yanzm
0
260
実践アプリケーション設計 ①データモデルとドメインモデル
recruitengineers
PRO
2
140
Exadata Database Service on Dedicated Infrastructure セキュリティ、ネットワーク、および管理について
oracle4engineer
PRO
1
360
Goss: New Production-Ready Go Binding for Faiss #coefl_go_jp
bengo4com
0
1.1k
知られざるprops命名の慣習 アクション編
uhyo
10
2.3k
Featured
See All Featured
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
161
15k
How to Think Like a Performance Engineer
csswizardry
25
1.8k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
8
480
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
31
2.2k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
The Straight Up "How To Draw Better" Workshop
denniskardys
236
140k
GraphQLとの向き合い方2022年版
quramy
49
14k
Rebuilding a faster, lazier Slack
samanthasiow
83
9.1k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.4k
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͓קΊͰ͢
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝· ͨ͠