django secret_key 需要提供加密签名(文档)。该值存储在 <your_app>/settings.py 文件中。当您启动新项目时,它将从内置函数(源代码)生成。对于生产部署,secret_key 必须强大且受到可靠保护。
这些步骤可帮助您生成新的 secret_key 值:
激活项目的虚拟环境:
source env/bin/activate
在django的manage.py shell中输入:
python3 manage.py shell
导入 get_random_secret_key() 函数:
from django.core.management.utils import get_random_secret_key
只需调用 get_random_secret_key() 函数即可:
get_random_secret_key()
完整示例:
user@localhost:~/demo$ source env/bin/activate (env) user@localhost:~/demo$ python3 manage.py shell Python 3.11.2 (main, Aug 26 2024, 07:20:54) [GCC 12.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from django.core.management.utils import get_random_secret_key >>> get_random_secret_key() '(#jdao11q1)kw1rs40z2$b^kntmw3ts9)wg2r*zk3z0_^t&hha'
我的帖子有帮助吗?您可以在 patreon 上支持我。
以上就是为 Django 项目的生产部署生成 SECRET_KEY的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号