Prep 02: Django Models and admin from MDN Tutorial Parts 3-4

MDN Django Tutorial Parts 3-4

Continuing to work in the repo you created for Prep 01.

Part 3

  1. Do Part 3 (Models)
  2. Question: what is a migration? did we need any yet? what alternative could there have been? how do they work?! e.g.
    1. how does django know what to put in a migration?
    2. how does django know which migrations have been applied?
  3. Read “Falsehoods Programmers Believe About Names – With Examples”
    1. Based on the points in the article, what (if any) changes might you propose to the models in this Prep?
    2. Have you ever been unsure how to enter information about yourself into a computer system?
      1. What information were you trying to enter?
      2. Do you recall what system?
      3. What was the cause of your uncertainty?
      4. (reflect on this personally, and if you have reflections that you wish to share that don’t doxx you, feel free to bring them to in-class discussion)
  4. Consider installing some database tools:
    1. GOAT: DBeaver Community (supports pretty much every kind of db ever)
    2. db-specific:
      1. DB Browser for SQLite
      2. PGAdmin for PostgresQL (we don’t need the PostgresQL stuff yet)

Best Practices: env files (for repeatability, secrets, and great good)

env files help track configuration information. Often these values may differ on different machines of your own or at least between you and a team member, and likely also between your machine for (local) development and your server(s) in the cloud that hosts your app.

Because it might be convenient to discard and recreate your database on occasion, and also to have consistent expectations for troubleshooting across all the students’ computers, make these files so that we don’t have to waste time waiting for you to re-find your django admin user’s password.

  1. Ensure that your .gitignore
    1. exists at all 😆
      1. if it doesn’t, maybe start from a good Python .gitignore template such as that provided by github
    2. has .env in it
  2. Create a file named .env.example in the root of your project (so it’s a sibling of catalog/, locllibrary_config/, and manage.py) with this code:
    DJANGO_SUPERUSER_USERNAME="me"
    DJANGO_SUPERUSER_PASSWORD="me"
    DJANGO_SUPERUSER_EMAIL="me@me.me"
    
  3. save a copy of that file as .env

We could add other things here, but this is all we need for now.

Instruct django project to use env file

  1. add django-environ as a dependency: uv add django-environ
  2. edit settings.py to
    1. import environ at the top
    2. immediately after the definition of BASE_DIR, add the following
      env = environ.Env()
      # FYI: OS environment variables take precedence over variables from .env
      env.read_env(str(BASE_DIR / ".env"))
      

create superuser with values from env

With this env file created and expected in your settings, you can add an argument to the django-admin createsuperuser command to have it default to these values:

uv run python manage.py createsuperuser --no-input

Part 4

  1. Do Part 4 (admin site).

Submitting

  1. Push your updated code back to your github repo
  2. submit the most recent commit URL to the gradescope assignment in a file called commit.url.
Last modified September 14, 2026: update preps (1c83e76)