Created
July 29, 2024 16:45
-
-
Save archatas/30115b2117f70c28ae0660c8df69ccdc to your computer and use it in GitHub Desktop.
get_secret() function that uses a JSON file for Django project secrets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
from django.core.exceptions import ImproperlyConfigured | |
with open(os.path.join(os.path.dirname(__file__), "secrets.json"), "r") as f: | |
secrets = json.loads(f.read()) | |
def get_secret(setting): | |
"""Get the secret variable or return explicit exception.""" | |
try: | |
return secrets[setting] | |
except KeyError: | |
error_msg = f"Set the {setting} secret variable" | |
raise ImproperlyConfigured(error_msg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment