17

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 admin@exmaple.com 
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?

Nick James
  • 173
  • 1
  • 1
  • 4
  • http://stackoverflow.com/questions/6244382/how-to-automate-createsuperuser-on-django – Lele Mar 02 '16 at 17:36

3 Answers3

23

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
David Dahan
  • 10,576
  • 11
  • 64
  • 137
23

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
Alexey Trofimov
  • 4,287
  • 1
  • 18
  • 27
0

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
  • 897
  • 8
  • 30