0Pricing
Django Academy · 课时

环境变量与配置拆分

安全地分离开发和生产配置

环境变量与配置拆分 是 CoddyKit 上的免费 Django Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Django Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Django Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

One Settings File Is Risky

A single settings.py with DEBUG on and secrets hardcoded is fine locally but dangerous live. Production needs its own safe configuration. 🔒

Dev and Prod Differ

Local and live environments need different settings: debug, allowed hosts, databases, and email all change between the two.

Why Environment Variables

Secrets like passwords must never sit in code or git. Environment variables inject them at runtime, keeping them out of your repository.

Read with os.environ

Pull a value from the environment with os.environ. Use a fallback so missing local variables do not crash development.

import os
SECRET_KEY = os.environ.get('SECRET_KEY', 'dev-only')

Load a .env File

Tools like python-dotenv load variables from a local .env file, so you do not export them by hand every session.

from dotenv import load_dotenv
load_dotenv()

Never Commit .env

Add .env to your .gitignore. Committing it would leak every secret, defeating the entire reason for using environment variables.

.env
*.pyc

The Settings Package

Split settings into a package: turn settings.py into a folder holding base, dev, and prod modules that share common config.

settings/
  __init__.py
  base.py
  dev.py
  prod.py

Share with base.py

Put everything common in base.py. The dev and prod files import from it, then override only what truly differs.

from .base import *
DEBUG = True

Lock Down prod.py

In prod.py keep DEBUG off and read SECRET_KEY plus ALLOWED_HOSTS from the environment, never from a literal in the file.

from .base import *
DEBUG = False
ALLOWED_HOSTS = os.environ['ALLOWED_HOSTS'].split(',')

Pick the Module

Choose which settings load with the DJANGO_SETTINGS_MODULE variable. Dev points to settings.dev while the server points to settings.prod.

export DJANGO_SETTINGS_MODULE=myproject.settings.prod

Typed Helpers Help

Libraries like django-environ parse env values into booleans, lists, and database URLs, so you avoid messy manual string conversions.

Quick Check

Where should your production SECRET_KEY come from?

Recap

You learned to split settings into base, dev, and prod, keep secrets in environment variables and .env, and select the module per environment. 🎯

常见问题解答

「环境变量与配置拆分」课时是免费的吗?

是的 — 「环境变量与配置拆分」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Django Academy 课程的其余内容,请升级到 CoddyKit PRO。 Django Academy 课程共包含 4 节课。

「环境变量与配置拆分」这节课中我会学到什么?

安全地分离开发和生产配置 你通过在浏览器中直接运行的动手代码来练习 Django Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Django Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Django Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「环境变量与配置拆分」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Django Academy 课中编写并运行代码吗?

能。每节 Django Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 Gunicorn 作为 WSGI 服务器
  2. 使用 Nginx 作为反向代理
  3. 使用 WhiteNoise 提供静态文件
  4. 环境变量与配置拆分
← 返回 Django Academy