Not able to create super user with Django manage.py
Asked Answered
W

8

80

Trying to create a super user for my database:

manage.py createsuperuser

Getting a sad recursive message:

Superuser creation skipped due to not running in a TTY. You can run manage.py createsuperuser in your project to create one manually.

Seriously Django? Seriously?

The only information I found for this was the one listed above but it didn't work: Unable to create superuser in django due to not working in TTY

And this other one here, which is basically the same: Can't Create Super User Django

Woolridge answered 11/9, 2015 at 22:15 Comment(6)
Are you using a virtual environment? What kind? Are you running this from the command line inside your virtualenv, or some other way like in an IDE?Procreate
@Procreate Running this from both PyCharm and plain terminal, no virtual environmentWoolridge
You get the same error in both?Procreate
@Procreate exactly the same messageWoolridge
For me on macOS, I needed to enable the Run Configuration option "Emulate terminal in output console". Fixed the problem in PyCharm 2019.1.3.Selhorst
Same here on Windows 10, Pycharm 2019.2Hebetate
A
172

If you run

$ python manage.py createsuperuser
Superuser creation skipped due to not running in a TTY. You can run manage.py createsuperuser in your project to create one manually.
from Git Bash and face the above error message try to append winpty i.e. for example:
$ winpty python manage.py createsuperuser
Username (leave blank to use '...'):

To be able to run python commands as usual on windows as well what I normally do is appending an alias line to the ~/.profile file i.e.

 MINGW64 ~$ cat ~/.profile
 alias python='winpty python'

After doing so, either source the ~/.profile file or simply restart the terminal and the initial command python manage.py createsuperuser should work as expected!

Alby answered 7/5, 2016 at 20:55 Comment(3)
this also works through docker if you run winpty docker-compose run python manage.py createsuperuserBootleg
Could you explain what winpty is and does?Conjectural
When I tried run above command it gives "No module named 'fcntl' " error. But fcntl is not available for windows. Any suggestion?Mccandless
C
17

In virtualenv, for creating super-user for Django project related to git-bash use the command:

winpty python manage.py createsuperuser.
Cuspid answered 13/4, 2020 at 3:12 Comment(1)
For more information refer here : $ winpty python manage.py createsuperuser //It should work by just appending 'winpty' before the command Source codegrepper.com/code-examples/whatever/….Retrogress
D
16

I had same problem when trying to create superuser in the docker container with command: sudo docker exec -i <container_name> sh. Adding option -t solved the problem:

sudo docker exec -it <container_name> sh

Dewhurst answered 13/4, 2019 at 12:29 Comment(0)
N
8

Since Django 3.0 you can create a superuser without TTY in two ways

Way 1: Pass values and secrets as ENV in the command line

    DJANGO_SUPERUSER_USERNAME=admin2 DJANGO_SUPERUSER_PASSWORD=psw \
    python manage.py createsuperuser [email protected] --noinput

Way 2: set DJANGO_SUPERUSER_PASSWORD as the environment variable

# .admin.env
DJANGO_SUPERUSER_PASSWORD=psw

# bash
source '.admin.env' && python manage.py createsuperuser --username=admin [email protected] --noinput

The output should say: Superuser created successfully.

Nomenclator answered 5/1, 2022 at 13:9 Comment(0)
M
4

To create an admin username and password, you must first use the command:

python manage.py migrate

Then after use the command:

python manage.py createsuperuser

Once these steps are complete, the program will ask you to enter:

  • username
  • email
  • password

With the password, it will not show as you are typing so it will appear as though you are not typing, but ignore it as it will ask you to renter the password. When you complete these steps, use the command:

python manage.py runserver

In the browser add "/admin", which will take you to the admin site, and then type in your new username and password.

Metallophone answered 15/11, 2019 at 18:2 Comment(0)
C
1

Check your docker-compose.yml file and make sure your django application is labeled by web under services.

Connection answered 12/11, 2021 at 8:4 Comment(0)
S
0

I tried creating superuser from Stash [ App: Pythonista on iOS ]

[ Make sure migrations are already made ]

$ django-admin createsuperuser
Swatow answered 20/11, 2020 at 5:12 Comment(0)
M
-3

I figured out how to do so. What I did was I went to VIEWS.py. Next, I imported the module os. Then I created a function called createSuperUser(request):. Then, I then created a variable called admin and set it equal to os.system("python manage.py createsuperuser"). Then after that, return admin. Finally, I restarted the Django site, then it will prompt you in the terminal.

import os

def createSuperUser(request):
    admin = os.system("python manage.py createsuperuser")
    return 
Metallophone answered 20/11, 2019 at 22:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.