Hambot
Hambot

Reputation: 155

How to to use django createsuperuser --noinput command

I'm fairly new to django and python and guess this is a basic question, but if anyone is able to tell me what I'm doing wrong and/or how to correctly struture a non-interactive django createsuperuser command (https://docs.djangoproject.com/en/3.1/ref/django-admin/#createsuperuser), I'd be really grateful. I would like to use this to create a superuser on a google cloud-run app I have running, and am trying to get this to work locally (on a seperate app created for testing purposes), but can't seem to. I've found surprisingly little about from quite a bit of googling and browsing Stackoverflow (probably because it's too obvious).

I have a basic django app that is working locally on Ubuntu with a CustomUser model in place. I'm able to create a superuser interactively without a problem. I have DJANGO_SUPERUSER_PASSWORD/EMAIL/USERNAME defined in my settings.py as detailed below (I know I will need to remove actual values in the future but just want to get it to work first).

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # Local
    'accounts',
]

AUTH_USER_MODEL = 'accounts.CustomUser'

DJANGO_SUPERUSER_PASSWORD = 'xxx'
DJANGO_SUPERUSER_EMAIL = '[email protected]'
DJANGO_SUPERUSER_USERNAME = 'xxx'

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'config.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'config.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators



My CustomUser model is as below (but allows interactive creation of a superuser):

from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    pass

I have tried various permutations of createsuperuser which don't work. The more sensible ones below (I think):

./manage.py createsuperuser --noinput --username DJANGO_SUPERUSER_USERNAME --email DJANGO_SUPERUSER_EMAIL --password DJANGO_SUPERUSER_PASSWORD
./manage.py createsuperuser --noinput --username USERNAME --email EMAIL --password PASSWORD 
./manage.py createsuperuser --noinput --username DJANGO_SUPERUSER_USERNAME --email DJANGO_SUPERUSER_EMAIL
./manage.py createsuperuser --noinput --username USERNAME --email EMAIL
./manage.py createsuperuser --noinput, username=DJANGO_SUPERUSER_USERNAME, email=DJANGO_SUPERUSER_USERNAME, password=DJANGO_SUPERUSER_PASSWORD

Any help/guidance/direction would be gratefully received!

Many thanks

Upvotes: 7

Views: 7213

Answers (2)

maerteijn
maerteijn

Reputation: 650

When you export the DJANGO_SUPERUSER_EMAIL / DJANGO_SUPERUSER_USERNAME / DJANGO_SUPERUSER_PASSWORD environment variables you can call manage.py with just the --noinput parameter:

$ export [email protected]
$ export DJANGO_SUPERUSER_USERNAME=admin
$ export DJANGO_SUPERUSER_PASSWORD=xxxxxxxx

$ ./manage.py createsuperuser --noinput

Instead of exporting the variables in your local environment you can also define them in the environment for a single command like so:

$ DJANGO_SUPERUSER_USERNAME=admin [email protected] DJANGO_SUPERUSER_PASSWORD=xxxxx ./manage.py createsuperuser --noinput

Upvotes: 10

user16748623
user16748623

Reputation: 56

The command createsuperuser can't use you settings.py to read your variables DJANGO_SUPERUSER_PASSWORD, DJANGO_SUPERUSER_EMAIL and DJANGO_SUPERUSER_USERNAME.

The command can only read environments variables. https://docs.djangoproject.com/en/3.2/ref/django-admin/#createsuperuser

You have to set your different environment variable first. Pay attention to the way you are setting your variables if you work with unix, windows or you are using it in a dockerfile.

[email protected]
export DJANGO_SUPERUSER_EMAIL

DJANGO_SUPERUSER_USERNAME=xxx
export DJANGO_SUPERUSER_USERNAME

DJANGO_SUPERUSER_PASSWORD=xxx
export DJANGO_SUPERUSER_PASSWORD

And then you can use the following command :

./manage.py createsuperuser --noinput --username $DJANGO_SUPERUSER_USERNAME --email $DJANGO_SUPERUSER_EMAIL --password $DJANGO_SUPERUSER_PASSWORD

There too, you have to look for the way you call environments variables depending on your OS.

Upvotes: 4

Related Questions