Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 991
Nick James Avatar asked Nov 16 '14 23:11

Nick James


People also ask

Which command is used to create a superuser in Django?

Type python3 manage.py createsuperuser in the given terminal and press “Enter”. The system will ask for credentials, after which a superuser will be created. To run the server, we type the command python3 manage.py runserver 0.0. 0.0:8000 and press “Enter”.

How do I run manage py in Runserver?

when you run python manage.py runserver, it will take 127.0. 0.1 as default ip address and 8000. 127.0. 0.0 is the same as localhost which can be accessed locally. to access it from cross origin you need to run it on your system ip or 0.0.

How do I uninstall superuser?

It is not possible to uninstall superuser directly from uninstaller because superuser is the main control app of your root files and access. But you can remove it by Factory reseting your phone ( make sure you backup all your apps and settings before doing so.) Factory reseting will default all your files and apps.


2 Answers

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
like image 106
David D. Avatar answered Sep 17 '22 11:09

David D.


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
like image 23
Alexey Trofimov Avatar answered Sep 17 '22 11:09

Alexey Trofimov