Nick James
Nick James

Reputation: 183

Django create superuser from batch file

I'm trying to make a batch file that runs syncdb to create a database file, and then create a superuser with the username "admin" and the password "admin".

My code so far:

python manage.py syncdb --noinput
python manage.py createsuperuser --username admin --email [email protected] 
python manage.py runserver

Now this prompts me to enter a password and then confirm the password. Can I enter this information with a command from the same batch file, another batch file, or is this just not possible?

Upvotes: 18

Views: 20437

Answers (3)

Alexey Trofimov
Alexey Trofimov

Reputation: 5017

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 protected]

or, using all environment variables:

DJANGO_SUPERUSER_PASSWORD=my_password \
DJANGO_SUPERUSER_USERNAME=my_user \
[email protected] \
./manage.py createsuperuser \
--no-input

Upvotes: 27

Nick T
Nick T

Reputation: 947

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', '[email protected]', 'mypwd')" | python manage.py shell

Upvotes: 1

David Dahan
David Dahan

Reputation: 11172

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', '[email protected]', 'pass')" | python manage.py shell
python manage.py runserver

Upvotes: 24

Related Questions