前提・実現したいこと
環境変数envについてのエラーを解決したいです。
はじめまして。Djangoを初めて触る初心者です。
base.pyにSTRIPE_PUBLIC_KEY = env("STRIPE_PUBLIC_KEY")を追加しrunserverしたところ
以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
raise ImproperlyConfigured(error_msg) django.core.exceptions.ImproperlyConfigured: Set the STRIPE_PUBLIC_KEY environment variable
該当のソースコード(base.py)最後の行にSTRIPE_PUBLIC_KEY=env("STRIPE_PUBLIC_KEY")はございます。
python
1""" 2Base settings to build other settings files upon. 3""" 4from pathlib import Path 5 6import environ 7 8 9ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent.parent 10# djgumroad/ 11APPS_DIR = ROOT_DIR / "djgumroad" 12env = environ.Env() 13 14READ_DOT_ENV_FILE = env.bool("DJANGO_READ_DOT_ENV_FILE", default=False) 15if READ_DOT_ENV_FILE: 16 # OS environment variables take precedence over variables from .env 17 env.read_env(str(ROOT_DIR / ".env")) 18 19# GENERAL 20# ------------------------------------------------------------------------------ 21# https://docs.djangoproject.com/en/dev/ref/settings/#debug 22DEBUG = env.bool("DJANGO_DEBUG", False) 23# Local time zone. Choices are 24# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name 25# though not all of them may be available with every OS. 26# In Windows, this must be set to your system time zone. 27TIME_ZONE = "UTC" 28# https://docs.djangoproject.com/en/dev/ref/settings/#language-code 29LANGUAGE_CODE = "en-us" 30# https://docs.djangoproject.com/en/dev/ref/settings/#site-id 31SITE_ID = 1 32# https://docs.djangoproject.com/en/dev/ref/settings/#use-i18n 33USE_I18N = True 34# https://docs.djangoproject.com/en/dev/ref/settings/#use-l10n 35USE_L10N = True 36# https://docs.djangoproject.com/en/dev/ref/settings/#use-tz 37USE_TZ = True 38# https://docs.djangoproject.com/en/dev/ref/settings/#locale-paths 39LOCALE_PATHS = [str(ROOT_DIR / "locale")] 40 41# DATABASES 42# ------------------------------------------------------------------------------ 43# https://docs.djangoproject.com/en/dev/ref/settings/#databases 44 45DATABASES = { 46 "default": env.db("DATABASE_URL", default="postgres:///djgumroad") 47} 48DATABASES["default"]["ATOMIC_REQUESTS"] = True 49 50 51# APPS 52# ------------------------------------------------------------------------------ 53DJANGO_APPS = [ 54 "django.contrib.auth", 55 "django.contrib.contenttypes", 56 "django.contrib.sessions", 57 "django.contrib.sites", 58 "django.contrib.messages", 59 "django.contrib.staticfiles", 60 # "django.contrib.humanize", # Handy template tags 61 "django.contrib.admin", 62 "django.forms", 63] 64THIRD_PARTY_APPS = [ 65 "crispy_forms", 66 "allauth", 67 "allauth.account", 68 "allauth.socialaccount", 69 "rest_framework", 70 "rest_framework.authtoken", 71 "corsheaders", 72 "tailwind", 73 "crispy_tailwind", 74] 75 76LOCAL_APPS = [ 77 "djgumroad.users.apps.UsersConfig", 78 "djgumroad.theme.apps.ThemeConfig", 79 "djgumroad.products.apps.ProductsConfig", 80 # Your stuff: custom apps go here 81] 82# https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps 83INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS 84 85# MIGRATIONS 86# ------------------------------------------------------------------------------ 87# https://docs.djangoproject.com/en/dev/ref/settings/#migration-modules 88MIGRATION_MODULES = {"sites": "djgumroad.contrib.sites.migrations"} 89 90 91 92# PASSWORDS 93# ------------------------------------------------------------------------------ 94# https://docs.djangoproject.com/en/dev/ref/settings/#password-hashers 95PASSWORD_HASHERS = [ 96 # https://docs.djangoproject.com/en/dev/topics/auth/passwords/#using-argon2-with-django 97 "django.contrib.auth.hashers.Argon2PasswordHasher", 98 "django.contrib.auth.hashers.PBKDF2PasswordHasher", 99 "django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher", 100 "django.contrib.auth.hashers.BCryptSHA256PasswordHasher", 101] 102# https://docs.djangoproject.com/en/dev/ref/settings/#auth-password-validators 103 104# MIDDLEWARE 105# ------------------------------------------------------------------------------ 106# https://docs.djangoproject.com/en/dev/ref/settings/#middleware 107MIDDLEWARE = [ 108 "django.middleware.security.SecurityMiddleware", 109 "corsheaders.middleware.CorsMiddleware", 110 "whitenoise.middleware.WhiteNoiseMiddleware", 111 "django.contrib.sessions.middleware.SessionMiddleware", 112 "django.middleware.locale.LocaleMiddleware", 113 "django.middleware.common.CommonMiddleware", 114 "django.middleware.csrf.CsrfViewMiddleware", 115 "django.contrib.auth.middleware.AuthenticationMiddleware", 116 "django.contrib.messages.middleware.MessageMiddleware", 117 "django.middleware.common.BrokenLinkEmailsMiddleware", 118 "django.middleware.clickjacking.XFrameOptionsMiddleware", 119] 120 121# STATIC 122# ------------------------------------------------------------------------------ 123# https://docs.djangoproject.com/en/dev/ref/settings/#static-root 124STATIC_ROOT = str(ROOT_DIR / "staticfiles") 125# https://docs.djangoproject.com/en/dev/ref/settings/#static-url 126STATIC_URL = "/static/" 127# https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS 128STATICFILES_DIRS = [str(APPS_DIR / "static")] 129# https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders 130STATICFILES_FINDERS = [ 131 "django.contrib.staticfiles.finders.FileSystemFinder", 132 "django.contrib.staticfiles.finders.AppDirectoriesFinder", 133] 134 135# MEDIA 136# ------------------------------------------------------------------------------ 137# https://docs.djangoproject.com/en/dev/ref/settings/#media-root 138MEDIA_ROOT = str(APPS_DIR / "media") 139# https://docs.djangoproject.com/en/dev/ref/settings/#media-url 140MEDIA_URL = "/media/" 141 142# TEMPLATES 143# ------------------------------------------------------------------------------ 144# https://docs.djangoproject.com/en/dev/ref/settings/#templates 145TEMPLATES = [ 146 { 147 # https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATES-BACKEND 148 "BACKEND": "django.template.backends.django.DjangoTemplates", 149 # https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs 150 "DIRS": [str(APPS_DIR / "templates")], 151 "OPTIONS": { 152 # https://docs.djangoproject.com/en/dev/ref/settings/#template-loaders 153 # https://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types 154 "loaders": [ 155 "django.template.loaders.filesystem.Loader", 156 "django.template.loaders.app_directories.Loader", 157 ], 158 # https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors 159 "context_processors": [ 160 "django.template.context_processors.debug", 161 "django.template.context_processors.request", 162 "django.contrib.auth.context_processors.auth", 163 "django.template.context_processors.i18n", 164 "django.template.context_processors.media", 165 "django.template.context_processors.static", 166 "django.template.context_processors.tz", 167 "django.contrib.messages.context_processors.messages", 168 "djgumroad.utils.context_processors.settings_context", 169 ], 170 }, 171 } 172] 173 174 175 176 177 178 179 180# django-rest-framework 181# ------------------------------------------------------------------------------- 182# django-rest-framework - https://www.django-rest-framework.org/api-guide/settings/ 183REST_FRAMEWORK = { 184 "DEFAULT_AUTHENTICATION_CLASSES": ( 185 "rest_framework.authentication.SessionAuthentication", 186 "rest_framework.authentication.TokenAuthentication", 187 ), 188 "DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAuthenticated",), 189} 190 191# django-cors-headers - https://github.com/adamchainz/django-cors-headers#setup 192CORS_URLS_REGEX = r"^/api/.*$" 193 194# django-tailwind - https://github.com/timonweb/django-tailwind#quick-start 195TAILWIND_APP_NAME = 'theme' 196 197#stripe 198 199STRIPE_PUBLIC_KEY = env("STRIPE_PUBLIC_KEY") 200STRIPE_SECRET_KEY = env("STRIPE_SECRET_KEY")
envファイル内(具体的なkeyは伏せさせていただきます。)
python
1 2STRIPE_PUBLIC_KEY=my_public_key 3STRIPE_SECRET_KEY=my_secret_key
試したこと
envファイルをbase.pyと同じディレクトリに移動させました。
environをインストールし直しました。
補足情報(FW/ツールのバージョンなど)
envファイル内のコードが青く表示されていないので、envファイル自体が機能していないのではないかと思っています。
また、base.pyのimport environをhoverするとImport "environ" could not be resolvedというメッセージが表示されます。environに原因があると思い、いろいろ調べたのですが解決に至りませんでした。
お力を貸していただけると幸いです。
django==3.0.11
django-environ==0.4.5
回答1件
あなたの回答
tips
プレビュー