1

我正在使用 Django 3.2 和 auth 模块。我想在命令行上创建一个超级用户(最终包含在 docker 脚本中)。当我尝试这个

$ python manage.py createsuperuser --username=joe --email=joe@example.com

系统提示我输入密码。该模块似乎不支持“--password”参数......

$ python manage.py createsuperuser --username=joe --email=joe@example.com --password=password
usage: manage.py createsuperuser [-h] [--username USERNAME] [--noinput] [--database DATABASE]
                                 [--email EMAIL] [--version] [-v {0,1,2,3}] [--settings SETTINGS]
                                 [--pythonpath PYTHONPATH] [--traceback] [--no-color] [--force-color]
                                 [--skip-checks]
manage.py createsuperuser: error: unrecognized arguments: --password=password

有没有一种方法可以在没有人工干预的情况下自动创建用户?

4

1 回答 1

2

这样做是出于安全原因:通常 shell 命令被写入历史文件,如果您因此将密码作为参数传递,黑客最终可能会查看历史记录,从而获得超级用户的密码。它可以读取DJANGO_SUPERUSER_PASSWORD变量的内容来设置密码。

因此,您可以使用以下方式设置密码:

DJANGO_SUPERUSER_PASSWORD=somepassword python manage.py createsuperuser --no-input --username=joe  --email=joe@example.com

但是,我强烈建议不要DJANGO_SUPERUSER_PASSWORD在脚本文件中设置,而是在其他地方定义环境变量。

于 2021-12-12T18:43:39.837 回答