Skip to content

Django

Reading list

Write your own commands

  • app_name/management/commands

Management commands (manage.py scripts)

Creating your custom commands

  1. Create a file called: app_name/management/commands/create_admin_superuser.py
  2. Fill in the blanks

When would you want to write your own command?

from django.core.management.base import BaseCommand

class Command(BaseCommand):
    help = """ help string """

    def add_arguments(self, parser):
        parser.add_argument("email", type=str)
        parser.add_argument("password", type=str)

    def handle(self, *args, **options):
        # do stuff
        self.stdout.write("Created an admin superuser")

Python Shell

python manage.py shell
import django
django.setup()
from django.contrib.auth.models import User, Group
# other imports

manage.py shell vs python console

  • manage.py shell sets the DJANGO_SETTINGS_MODULEwhich lets it know about the settings.py

(django_extensions has a shell_plus)

django_extensions

video

adds a bunch of nice helpers to ./manage.py

  • graph_models
    • graphs your models
  • runserver_plus
    • django error page with a Python terminal to see the info
  • runscript
    • run a python file that sets up django (import django, django.setup())
    • nice for scripts that are run with cron
  • shell_plus
    • django shell that loads all of your models
  • print_user_for_session <session_ID>
    • get all of the session info for a user

Environment variables

SECRET_KEY


Last update: 2022-09-23