Skip to content

Instantly share code, notes, and snippets.

@bryanchow
Last active June 14, 2022 05:07
Show Gist options
  • Save bryanchow/d30c0c7021f93b416744 to your computer and use it in GitHub Desktop.
Save bryanchow/d30c0c7021f93b416744 to your computer and use it in GitHub Desktop.
# https://gist.github.com/bryanchow/d30c0c7021f93b416744
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
def require_settings(*args):
"""
Check django.settings for the presence of the specified keys. Accepts
settings keys as function args, or as a list of strings.
Usage:
require_settings('SITE_URL', 'DEFAULT_DOMAIN')
require_settings(['SITE_URL', 'DEFAULT_DOMAIN'])
"""
if len(args) == 1 and not isinstance(args[0], str):
args = args[0]
for key in args:
if not hasattr(settings, key):
raise ImproperlyConfigured(
"{} not defined in settings".format(key)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment