上传项目后在github上的django项目中隐藏密钥

ami*_*aji 4 python django

我在 github 上上传了我的 django 项目,并且我的项目有很多提交。

我不想删除我的项目并重新上传。

将项目上传到 github 并进行大量提交后,隐藏密钥的最简单方法是什么?

Tms*_*s91 8

manage.pyis所在的同目录下,创建一个文件名.env,并放入其中

SECRET_KEY = '....your secret key ....' # --- the one indicated in your settings.py, cut an paste it here
Run Code Online (Sandbox Code Playgroud)

在同一目录下,创建一个名为 的文件.gitignore,并放入

.env
Run Code Online (Sandbox Code Playgroud)

在里面。然后在您settings.py添加:

from decouple import config

SECRET_KEY = config("SECRET_KEY") # this is to replace the secret key you cut away before
Run Code Online (Sandbox Code Playgroud)

然后在您的命令提示符中运行:

pip install python-decouple
pip freeze>requirements.txt
Run Code Online (Sandbox Code Playgroud)

然后在github上添加、提交和推送。

在这里您可以找到有关.gitignore如何工作的更多信息。