Challenge 2: Write a Gunicorn + Static Files Deployment Sequence — Possible Solution ==================================================================== # 1. Pull the latest code git pull origin main # 2. Install/update dependencies (in the project's virtual environment) pip install -r requirements.txt # 3. Apply any pending database migrations python manage.py migrate # 4. Collect static files into STATIC_ROOT (so nginx/WhiteNoise can serve # the LATEST versions, including anything changed in this deploy) python manage.py collectstatic --noinput # 5. Run Django's deployment checklist as a final sanity check python manage.py check --deploy # 6. Restart Gunicorn so it picks up the new code # (exact command depends on your process manager — systemd example:) sudo systemctl restart gunicorn WHY THIS ORDER MATTERS -------------------------- - migrate runs BEFORE the app restarts, so the database schema is already in the state the new code expects by the time Gunicorn starts serving requests with it — restarting first would risk the new code querying columns/tables that don't exist yet. - collectstatic runs BEFORE restarting Gunicorn specifically because static files need to be in place and current before real traffic hits the new code — if this step were skipped or run after the restart, users could briefly (or indefinitely, if forgotten entirely) receive 404s for CSS/JS that the new templates now reference. - check --deploy runs as a final gate BEFORE actually restarting the live process — if it surfaces something serious (DEBUG accidentally left True, a missing security setting), you still have the chance to fix it before the new code goes live, rather than after. - Restarting Gunicorn last means every other step (dependencies, migrations, static files, the deploy check) has already succeeded by the time the actual cutover to new code happens — minimizing the window where something could go wrong mid-restart. WHY THIS WORKS -------------- - This sequence directly mirrors the "test everything before it's deployable" philosophy from the TypeScript course's own Deployment & CI/CD chapter — type-check/test/build all had to succeed before a deployable artifact even existed there; here, migrations/static files/deploy-check all need to succeed before the live process restarts. - --noinput on collectstatic prevents it from pausing to ask for confirmation about overwriting existing files — necessary for this to run unattended as part of an automated deployment script rather than requiring a human to type "yes" at a prompt.