pythonanywhere的django settings.py

Y.N*_*Y.N 3 django-settings django-staticfiles pythonanywhere

我有 django 应用程序,我在本地开发并部署到 pythonanywhere。

本地 Setting.py 包含 sqlite-db、本地静态路径等。 pythonanywhere 上的 Setting.py 包含 mysql-db、静态的 cdn 链接等。

我想在我的 git 中有一个设置文件,有一些像这样的检查

if host_type=='pythonanywhere':
    DATABASES = { "default": { "ENGINE": "django.db.backends.mysql",
Run Code Online (Sandbox Code Playgroud)

为此,settings.py 的最佳实践在哪里?

pythonanywhere 提供了哪些 os.environ?

hwj*_*wjp 5

PythonAnywhere 没有为您提供用于设置环境变量的 UI,尽管您可以编辑您的/home/yourusername/.bashrc文件并使用

export MY_ENV_VAR="something"
Run Code Online (Sandbox Code Playgroud)

从您的问题来看,我猜您想避免将数据库设置存储在版本控制中。因此,作为环境变量的替代方案,我使用的一种技术是在版本控制之外创建一个包含自定义设置的文件:

echo "production_settings.py" >> .gitignore
Run Code Online (Sandbox Code Playgroud)

在 settings.py 中:

#... default dev settings
DATABASES = { "default": {"ENGINE": "django.db.backends.sqlite3" 
#...
try:
    from production_settings import DATABASES
except ImportError:
    pass
Run Code Online (Sandbox Code Playgroud)

因此,在 PythonAnywhere 上,您可以将生产设置保存在名为 production_settings.py 的文件中,而在您的开发机器上,您什么都不做。

您也可以使用此方法SECRET_KEY在服务器上设置自定义,这将是一个很好的安全实践......