使用 Django 配置 Google Cloud App Engine
Posted
技术标签:
【中文标题】使用 Django 配置 Google Cloud App Engine【英文标题】:Configuring Google Cloud App Engine w Django 【发布时间】:2021-09-12 14:13:41 【问题描述】:我正在关注这个tutorial 使用 Google Cloud 部署 Django。在时间戳 (13:39) 时,他进入 VS Code 中 Django 项目的 settings.py,并将一些占位符换成了他自己的 Google Cloud 凭据。然而,我的 settings.py 看起来非常不同。块帽中唯一看起来像是占位符的东西是“GOOGLE_CLOUD_PROJECT”和“SETTINGS_NAME”。这是我无法通过的安装部分,因为在下一步我尝试执行时
python manage.py makemigrations
我被派到这里:
else:
raise Exception("No local .env or GOOGLE_CLOUD_PROJECT detected. No secrets found.")
希望有人可以浏览我的 settings.py 并找出我错过了什么,为什么那个 if 块会变成 Exception。
需要注意的一点:***导入 environ 和 google.cloud 未解析。 idk,如果这与它有任何关系。文件就是这样从罐子里出来的。我不想弄乱它,以防 tshtf :D
settings.py
import io
import os
import environ
from google.cloud import secretmanager
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# [START gaestd_py_django_secret_config]
env = environ.Env(DEBUG=(bool, False))
env_file = os.path.join(BASE_DIR, ".env")
if os.path.isfile(env_file):
# Use a local secret file, if provided
env.read_env(env_file)
# [START_EXCLUDE]
elif os.getenv("TRAMPOLINE_CI", None):
# Create local settings if running with CI, for unit testing
placeholder = (
f"SECRET_KEY=a\n"
f"DATABASE_URL=sqlite://os.path.join(BASE_DIR, 'db.sqlite3')"
)
env.read_env(io.StringIO(placeholder))
# [END_EXCLUDE]
elif os.environ.get("GOOGLE_CLOUD_PROJECT", None):
# Pull secrets from Secret Manager
project_id = os.environ.get("GOOGLE_CLOUD_PROJECT")
client = secretmanager.SecretManagerServiceClient()
settings_name = os.environ.get("SETTINGS_NAME", "django_settings")
name = f"projects/project_id/secrets/settings_name/versions/latest"
payload = client.access_secret_version(name=name).payload.data.decode("UTF-8")
env.read_env(io.StringIO(payload))
else:
raise Exception("No local .env or GOOGLE_CLOUD_PROJECT detected. No secrets found.")
# [END gaestd_py_django_secret_config]
SECRET_KEY = env("SECRET_KEY")
# SECURITY WARNING: don't run with debug turned on in production!
# Change this to "False" when you are ready for production
DEBUG = True
# SECURITY WARNING: App Engine's security features ensure that it is safe to
# have ALLOWED_HOSTS = ['*'] when the app is deployed. If you deploy a Django
# app not on App Engine, make sure to set an appropriate host here.
ALLOWED_HOSTS = ["*"]
# Application definition
INSTALLED_APPS = [
"polls.apps.PollsConfig",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
]
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
ROOT_URLCONF = "mysite.urls"
TEMPLATES = [
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS":
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
,
,
]
WSGI_APPLICATION = "mysite.wsgi.application"
# Database
# [START db_setup]
# [START gaestd_py_django_database_config]
# Use django-environ to parse the connection string
DATABASES = "default": env.db()
# If the flag as been set, configure to use proxy
if os.getenv("USE_CLOUD_SQL_AUTH_PROXY", None):
DATABASES["default"]["HOST"] = "127.0.0.1"
DATABASES["default"]["PORT"] = 5432
# [END gaestd_py_django_database_config]
# [END db_setup]
# Use a in-memory sqlite3 database when testing in CI systems
# TODO(glasnt) CHECK IF THIS IS REQUIRED because we're setting a val above
if os.getenv("TRAMPOLINE_CI", None):
DATABASES =
"default":
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
# Password validation
AUTH_PASSWORD_VALIDATORS = [
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", # noqa: 501
,
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", # noqa: 501
,
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", # noqa: 501
,
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", # noqa: 501
,
]
# Internationalization
LANGUAGE_CODE = "en-us"
TIME_ZONE = "UTC"
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, javascript, Images)
STATIC_ROOT = "static"
STATIC_URL = "/static/"
STATICFILES_DIRS = []
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
【问题讨论】:
【参考方案1】:Django 希望你提供一个SECRET_KEY
。有几种方法可以提供这样的密钥,但一种方法是在 manage.py
文件所在的同一文件夹中创建一个包含 SECRET_KEY
的 .env
文件。
可以使用以下code snippet 生成SECRET_KEY
(在您认为方便的地方运行它,但不要将生成的密钥字符串添加到您的settings.py
文件中):
from django.core.management.utils import get_random_secret_key
get_random_secret_key()
获取结果输出(它是一个 50 个字符长的字符串)并将其添加到 .env
文件中,格式如下:
SECRET_KEY="the-50-character-string-here"
您可能还必须将settings.py
文件中的BASE_DIR
替换为以下内容,以便正确加载.env
文件(注意第一行是如何被注释掉的)。
#BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.getcwd()
您可能会遇到更多与数据库设置相关的问题。查看this 和this SO 问题。
您还可以考虑查看Guide,这是您所指的教程所遵循的内容。
【讨论】:
以上是关于使用 Django 配置 Google Cloud App Engine的主要内容,如果未能解决你的问题,请参考以下文章
在上载到google cloud storage django之前调整图像大小
如何在 Google.Cloud.PubSub.V1 SubscriberServiceApiClientBuilder 中配置频道选项
使用 Google Cloud Storage PHP API 设置 CORS 配置不起作用
如何使用 Google Cloud 功能和 MQTT 桥接器更新设备配置