[Django]-What is my user and pass? Deploy django on AWS EC2 but can not login admin

5πŸ‘

βœ…

In order to create an admin user for my Django app on Beanstalk I created a custom Django command that I invoke in containers_commands, so there is no need for human input at all! Moreover I defined the user/password as environment variables so I can put my code under version control safely. The implementation of my command is something similar to this:

import os

from django.core.management.base import BaseCommand

from com.cygora.apps.users.models.User import User

class Command(BaseCommand):

    def handle(self, *args, **options):
        username = os.environ['SUPER_USER_NAME']
        if not User.objects.filter(username=username).exists():
            User.objects.create_superuser(username,
                                          os.environ['SUPER_USER_EMAIL'],
                                          os.environ['SUPER_USER_PASSWORD'])

then in my beanstalk config:

container_commands:
  02_create_superuser_for_django_admin:
    command: "python manage.py create_cygora_superuser"
    leader_only: true

ps: if you never created a custom Django commands before, all you have to to is to create a package: management.commands in your desired app (ie: /your_project/your_app/management/commands/the_command.py), Django will load it automatically (and you can see it when typing python manage.py --help)

πŸ‘€daveoncode

Leave a comment