Prep 02: Django Models and admin from MDN Tutorial Parts 3-4
Categories:
3 minute read
Continuing to work in the repo you created for Prep 01.
Part 3
- Do Part 3 (Models)
- Question: what is a migration? did we need any yet? what alternative could there have been? how do they work?! e.g.
- how does django know what to put in a migration?
- how does django know which migrations have been applied?
- Read “Falsehoods Programmers Believe About Names – With Examples”
- Based on the points in the article, what (if any) changes might you propose to the models in this Prep?
- Have you ever been unsure how to enter information about yourself into a computer system?
- What information were you trying to enter?
- Do you recall what system?
- What was the cause of your uncertainty?
- (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)
- Consider installing some database tools:
- GOAT: DBeaver Community (supports pretty much every kind of db ever)
- db-specific:
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.
env files can help us in being safe in production, but below I’m proposing that on our local django projects (that no one else can reach anyway) we will all use the same weak password.
I WOULD NEVER TELL YOU TO DO THAT IN PRODUCTION!
- Ensure that your
.gitignore- exists at all 😆
- if it doesn’t, maybe start from a good Python .gitignore template such as that provided by github
- has
.envin it
- exists at all 😆
- Create a file named
.env.examplein the root of your project (so it’s a sibling ofcatalog/,locllibrary_config/, andmanage.py) with this code:DJANGO_SUPERUSER_USERNAME="me" DJANGO_SUPERUSER_PASSWORD="me" DJANGO_SUPERUSER_EMAIL="me@me.me" - 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
- add django-environ as a dependency:
uv add django-environ - edit
settings.pytoimport environat the top- immediately after the definition of
BASE_DIR, add the followingenv = 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
- Do Part 4 (admin site).
Submitting
- Push your updated code back to your github repo
- submit the most recent commit URL to the gradescope assignment in a file called
commit.url.