Upgrade to Pro — share decks privately, control downloads, hide ads and more …

本番運用を想定したDjango settings.pyの書き方入門

本番運用を想定したDjango settings.pyの書き方入門

Open Source Conference 2021 Online Nagoya資料
https://event.ospn.jp/osc2021-online-nagoya/session/312812

Ryuji Tsutsui

May 29, 2021
Tweet

More Decks by Ryuji Tsutsui

Other Decks in Programming

Transcript

  1. 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ϓϩδΣΫτશମͰࢀরͰ͖Δάϩʔόϧͳ ఆ਺ΛఆٛͰ͖Δ
  2. ͜Μͳ࣌Ͳ͏͢Ε͹͍͍ͷͩΖ͏…ʁ 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', # ຊ൪؀ڥ͔͠࢖Θͳ͍ઃఆ ] ։ൃ؀ڥɾຊ൪؀ڥͲͪΒʹͱͬͯ΋ෆཁͳઃఆ ؚ͕·Ε͍ͯΔʜ
  3. ΞϓϦىಈલʹෆཁͳߦΛίϝϯτΞ΢τ͢Δʁ 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', # ຊ൪؀ڥ͔͠࢖Θͳ͍ઃఆ ] ϛε͕͋Γͦ͏ͳͷͰ΍Γͨ͘ͳ͍
  4. ༗ޮແޮΛ੾Γସ͑ΔΦϓγϣϯΛ༻ҙ͢Δʁ MODE = ... # ؀ڥม਺͔MODEΛఆٛͨ͠ผͷϑΝΠϧΛಡΈࠐΉ INSTALLED_APPS = [ 'django.contrib.admin',

    'django.contrib.auth', # ʢলུʣ ] if MODE == 'local': INSTALLED_APPS += ['debug_toolbar'] ίϝϯτΞ΢τΑΓ͸ΦϖϛεͷՄ ೳੑ͸௿ͦ͏͚ͩͲʜ
  5. ༗ޮແޮΛ੾Γସ͑ΔΦϓγϣϯΛ༻ҙ͢Δʁ 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': ... Φϓγϣϯ͕૿͑Δͱઃఆ͕ෳࡶԽ ͦ͠͏
  6. ͜ͷΑ͏ͳߏ੒͕͓קΊ django_example #"" settings # settings.py͕͋ͬͨ৔ॴʹ͜ΕΛ࡞Δ !"" __init__.py !"" base.py

    # ͢΂ͯͷ؀ڥͰ࢖͏ڞ௨ͷઃఆ !"" local.py # ։ൃ؀ڥ༻ͷઃఆ !"" production.py # ຊ൪؀ڥ༻ͷઃఆ #"" staging.py # εςʔδϯά؀ڥ༻ͷઃఆ TFUUJOHTQZ͸ফ͢
  7. ֤ϑΝΠϧͷ಺༰ INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', # ʢলུʣ ] ROOT_URLCONF

    = 'django_example.urls' WSGI_APPLICATION = 'django_example.wsgi.application' base.pyʢڞ௨ͷઃఆʣ
  8. 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ʢຊ൪؀ڥ༻ʣ ͜͜Ͱڞ௨ͷઃఆ ΛಡΈࠐΉ
  9. 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ʯʹมߋ
  10. ඞਢͷ؀ڥม਺͸Ͳ͏΍ͬͯॻ͘ʁ 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ʯ ϖʔδ
  11. Djangoͷϩάઃఆͷ΍Γํ • LOGGINGͱ͍͏߲໨ʹࣙॻܕͰઃఆΛॻ͖·͢ • ʢͬ͘͟Γઆ໌͢ΔͱʣϩάΛͲ͜ʹग़ྗ͢Δ͔Λ͜͜ͰܾΊ·͢ • ग़ྗઌͷྫ • ϝʔϧ •

    ඪ४ग़ྗ • ϩʔΧϧϑΝΠϧ • ϩάϩʔςʔγϣϯʢݹ͍ϩάͷΞʔΧΠϒ΍࡟আʣ΋Ͱ͖·͢ • ʮϩάϨϕϧʯʢDEBUG, INFO, WARNING, ERROR, CRITICALʣ΍ϩάग़ྗݩͷϞδϡʔϧʹ Αͬͯग़ྗઌΛ੾Γସ͑Δ͜ͱ΋Ͱ͖·͢
  12. 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