Is it possible to create Django Admin account with free plan?

The tutorial Getting Started with Django on Render | Render says that creation of a Django Admin account can be done with the Render Shell.
But the Render Shell is not available for free plan services.
Is there any other alternatives methods to create Django Admin account with free plan?

2 Likes

Hi there,

Thanks for reaching out.

You could probably workaround this by adding it to your build script to run the admin creation command:

python manage.py createsuperuser

To make sure itā€™s not run on every deploy, maybe wrap it in an env var check, maybe something like:

if [[-z $CREATE_SUPERUSER]]; then python manage.py createsuperuserfi

Set the env var CREATE_SUPERUSER, deploy to have the command run, then remove the CREATE_SUPERUSER env var, so itā€™s not run again.

Hope that helps

Alan

Thanks!

But how it is supposed to work if the createsuperuser command is interactive?

Iā€™ve tried to add your snippet to my ā€˜build.shā€™ script, but this just gives me this message in the logs:

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

Ah apologies, Iā€™m not a Python/Django expert.

A quick search seemed to come up with a workaround: https://vuyisile.com/how-to-automate-creating-a-django-super-user/

Alan

I went through the link and was able to solve the same issue I had. So basically, you just add the superuser email, username(this wasnā€™t specified in the link but i think its equally necessary in order to log in as the admin login page requires a ā€œusernameā€ and password) and password as environmental variables. Then include the ā€œpython manage.py createsuperuser --no-inputā€ command in your build script. This forces django to look for the credentials as environmental variables. Note the risk in exposing sensitive credentials by supplying them as environmental variables.

All the hints are given, but Iā€™ve received a request to write the complete solution to this problem, so here it is.

Go to the dashboard of your project and create the following environment variables:

Key = Value
-----------------
CREATE_SUPERUSER = True
DJANGO_SUPERUSER_EMAIL = <...>
DJANGO_SUPERUSER_PASSWORD = <...>
DJANGO_SUPERUSER_USERNAME =<...>

After that add something like this to your build.sh script:

if [[ $CREATE_SUPERUSER ]];
then
  python world_champ_2022/manage.py createsuperuser --no-input
fi

Commit and push your changes. CI pipeline should create the superuser now. Check your website to verify this.

If everything is ok, you can drop CREATE_SUPERUSER variable from your environment, so the snippet in the build.sh wouldnā€™t execute on your next builds.

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.