[Fixed]-Django create superuser from batch file

24👍

As it seems you can’t provide a password with the echo ''stuff | cmd, the only way I see to do it is to create it in Python:

python manage.py syncdb --noinput
echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'admin@example.com', 'pass')" | python manage.py shell
python manage.py runserver

24👍

As of Django 3.0 (per the docs) you can use the createsuperuser --no-input option and set the password with the DJANGO_SUPERUSER_PASSWORD environment variable, e.g.,

DJANGO_SUPERUSER_PASSWORD=my_password ./manage.py createsuperuser \
    --no-input \
    --username=my_user \
    --email=my_user@domain.com

or, using all environment variables:

DJANGO_SUPERUSER_PASSWORD=my_password \
DJANGO_SUPERUSER_USERNAME=my_user \
DJANGO_SUPERUSER_EMAIL=my_user@domain.com \
./manage.py createsuperuser \
--no-input

1👍

And if as is good practice you are using a custom user (CustomUser in this case)

echo "from django.contrib.auth import get_user_model; CustomUser = get_user_model();  CustomUser.objects.create_superuser('me', 'nt@example.co.uk', 'mypwd')" | python manage.py shell
👤Nick T

Leave a comment