2017-06-29 39 views
2

最近,我正在與docker部署一個應用程序。我遇到了一個關於如何編寫將在Linux上執行的啓動腳本的問題。一個例子是老鄉:如何使用Linux shell腳本在Django中創建超級用戶?

通常情況下,我們建立這樣的超級用戶:

iMac:mysite yetongxue$ python manage.py createsuperuser 
Username (leave blank to use 'yetongxue'): root 
Email address: [email protected] 
Password: 
Password (again): 
Superuser created successfully. 

但是當搬運工部署應用程序,我必須在腳本將泊塢窗容器開始後執行寫這個命令。如何在腳本中設置用戶名,電子郵件和密碼?謝謝!

#!/bin/bash 

python manage.py createsuperuser 

回答

9

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

您需要爲auth.user型號提供夾具。對於密碼,您可以生成一個通過Django的殼

>>> 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 
     } 
    } 
] 
+0

是,這是一個有益的方法。謝謝。 –

相關問題