【问题标题】:How to create a superuser in Django with Linux shell script?如何使用 Linux shell 脚本在 Django 中创建超级用户?
【发布时间】:2017-12-02 21:11:25
【问题描述】:

最近,我正在尝试使用 docker 部署一个应用程序。我遇到了一个关于如何编写将在 Linux 上执行的启动脚本的问题。一个例子是:

通常,我们这样创建超级用户:

iMac:mysite yetongxue$ python manage.py createsuperuser
Username (leave blank to use 'yetongxue'): root
Email address: example@qq.com
Password: 
Password (again): 
Superuser created successfully.

但是当使用 docker 部署应用程序时,我必须在脚本中编写此命令,该脚本将在 docker 容器启动后执行。如何在脚本中设置用户名、电子邮件和密码?谢谢!

#!/bin/bash

python manage.py createsuperuser

【问题讨论】:

    标签: linux django shell docker


    【解决方案1】:

    而不是使用createsuperuser管理命令你可以provide a fixture。

    您需要为auth.user 模型提供一个夹具。对于密码,您可以通过 Django shell 生成一个

    >>> from django.contrib.auth.hashers import make_password
    >>> make_password('password')
    

    您的最终夹具应如下所示:

    [
        { "model": "auth.user",
            "pk": 1,
            "fields": {
                "username": "admin",
                "password": "<output of make_password()>"
                "is_superuser": true,
                "is_staff": true,
                "is_active": true
            }
        }
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-03
      • 2021-10-13
      • 1970-01-01
      • 2011-03-22
      • 2015-02-17
      • 1970-01-01
      相关资源
      最近更新 更多