Django create superuser from batch file
Asked Answered
C

3

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 [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?

Ciprian answered 16/11, 2014 at 23:43 Comment(1)
#6244882Apeman
M
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', '[email protected]', 'pass')" | python manage.py shell
python manage.py runserver
Mccurry answered 16/11, 2014 at 23:58 Comment(4)
I tried what you suggested, and it is still prompting me for a password as if the echo did not feed it.Ciprian
My mistake, it's probably impossible since it's a password. I have updated my answer with something else you can tryMccurry
I wonder when they will change this mess, would be nice to do this properly one dayAvowed
This gotta be most useful hack for testing python app at initial stateCalicle
P
25

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
Phenanthrene answered 24/12, 2019 at 10:20 Comment(1)
It should be DJANGO_SUPERUSER_USERNAMEAlvey
M
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', '[email protected]', 'pass')" | python manage.py shell
python manage.py runserver
Mccurry answered 16/11, 2014 at 23:58 Comment(4)
I tried what you suggested, and it is still prompting me for a password as if the echo did not feed it.Ciprian
My mistake, it's probably impossible since it's a password. I have updated my answer with something else you can tryMccurry
I wonder when they will change this mess, would be nice to do this properly one dayAvowed
This gotta be most useful hack for testing python app at initial stateCalicle
S
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', '[email protected]', 'mypwd')" | python manage.py shell
Sanborne answered 7/7, 2020 at 8:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.