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

Python 上传项目后在github上隐藏django项目中的密钥,python,django,Python,Django,我把我的django项目上传到github上,我对我的项目有很多承诺 我不想删除我的项目并重新加载它 将项目上传到github并多次提交后,隐藏密钥的最简单方法是什么?在同一个目录中,即manage.py,创建一个名为.env的文件,并将其放入其中 SECRET_KEY = '....your secret key ....' # --- the one indicated in your settings.py, cut an paste it here .env 在同一目录中,创建一个名

我把我的django项目上传到github上,我对我的项目有很多承诺

我不想删除我的项目并重新加载它


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

在同一个目录中,即
manage.py
,创建一个名为
.env
的文件,并将其放入其中

SECRET_KEY = '....your secret key ....' # --- the one indicated in your settings.py, cut an paste it here
.env
在同一目录中,创建一个名为
.gitignore
的文件,并将其放入其中

SECRET_KEY = '....your secret key ....' # --- the one indicated in your settings.py, cut an paste it here
.env
然后在
settings.py中添加:

from decouple import config

SECRET_KEY = config("SECRET_KEY") # this is to replace the secret key you cut away before
然后在命令提示中键入:

pip install python-decouple
pip freeze>requirements.txt

然后添加、提交并推送github。

这是公共回购还是私有回购?您应该使用类似于
secret\u key=os.environ.get('secret\u key',owugnweufgnw')的方式设置密钥。
;这样,您就可以在运行它的每个环境中设置不同的密钥,并在repo中设置一个无意义的默认值。使用python解耦并将密钥保存在
.env
中,不要忘记将.env放入.gitignore!