Skip to content

Instantly share code, notes, and snippets.

@archatas
Created July 29, 2024 16:45
Show Gist options
  • Save archatas/30115b2117f70c28ae0660c8df69ccdc to your computer and use it in GitHub Desktop.
Save archatas/30115b2117f70c28ae0660c8df69ccdc to your computer and use it in GitHub Desktop.
get_secret() function that uses a JSON file for Django project secrets
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