Django, gestion des settings d'application simplifiée
source link: https://blog.mathieu-leplatre.info/django-gestion-des-settings-dapplication-simplifiee-fr.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Django, gestion des settings d'application simplifiée
Fri 29 July 2011Je reprends ici la méthode présentée par Nicolas et je la couple avec mon petit EasyDict pour alléger l'utilisation ! Comme ses snippets sont drôles, je ne les change pas !
Paramètres par défault de l'application
On a juste un constructeur à ajouter par rapport à ce qu'avait présenté NiKo (avec EasyDict installé).
# apps/my_app/__init__.py from django.conf import settings from easydict import EasyDict app_settings = EasyDict(dict({ 'FOO': 42, 'ENABLE_CHUCK_NORRIZ_MODE': False, }, **getattr(settings, 'MY_APP_CONFIG', {})))
Surcharge dans le projet
# settings.py MY_APP_CONFIG = { 'ENABLE_CHUCK_NORRIZ_MODE': True, }
Utilisation !
EasyDict transforme les clés du dict en attributs, on accède aux settings en toute simplicité !
# foo/bar.py from my_app import app_settings print app_settings.FOO # 42
# apps/my_app/utils.py from . import app_settings if app_settings.ENABLE_CHUCK_NORRIZ_MODE: print 'Chuck Norriz is watching you' else: print 'Dance dance, little lamb'
#django, #easydict - Posted in the Dev category
© Copyright 2020 by Mathieu Leplatre. mnmlist Theme
Content licensed under the Creative Commons attribution-noncommercial-sharealike License.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK