|
| 1 | +# Module 10 / Project 01 — Django Setup |
| 2 | + |
| 3 | +Home: [README](../../../../README.md) |
| 4 | + |
| 5 | +## Focus |
| 6 | + |
| 7 | +Creating a Django project from scratch: `startproject`, `startapp`, models, admin, migrations, and `runserver`. |
| 8 | + |
| 9 | +## Why this project exists |
| 10 | + |
| 11 | +Every Django application starts the same way: you run `startproject`, then `startapp`, define models, create migrations, and start the development server. This project walks you through that process step by step. Instead of running Django commands blindly, you will use a guided setup script that creates the project structure and explains every generated file along the way. |
| 12 | + |
| 13 | +Understanding the file structure Django creates is essential. Each file has a specific purpose, and knowing what goes where will save you hours of confusion later. |
| 14 | + |
| 15 | +## Run |
| 16 | + |
| 17 | +```bash |
| 18 | +cd projects/modules/10-django-fullstack/01-django-setup |
| 19 | +python setup_guide.py |
| 20 | +``` |
| 21 | + |
| 22 | +The script creates a Django project and app, then prints an explanation of every file it generated. After running it, explore the created directory structure. |
| 23 | + |
| 24 | +To verify the generated project works: |
| 25 | + |
| 26 | +```bash |
| 27 | +cd demo_project |
| 28 | +python manage.py migrate |
| 29 | +python manage.py createsuperuser |
| 30 | +python manage.py runserver |
| 31 | +``` |
| 32 | + |
| 33 | +Then open your browser to: |
| 34 | + |
| 35 | +- **http://127.0.0.1:8000** — Django welcome page |
| 36 | +- **http://127.0.0.1:8000/admin** — Django admin interface (log in with the superuser you created) |
| 37 | + |
| 38 | +Press `Ctrl+C` to stop the server. |
| 39 | + |
| 40 | +## Expected output |
| 41 | + |
| 42 | +Running `setup_guide.py` prints explanations like: |
| 43 | + |
| 44 | +```text |
| 45 | +=== Django Project Setup Guide === |
| 46 | +
|
| 47 | +[1/6] Creating project directory: demo_project/ |
| 48 | + Created: demo_project/manage.py |
| 49 | + -> The command-line entry point for your Django project. |
| 50 | + Run migrations, start the server, create apps — all through manage.py. |
| 51 | +
|
| 52 | + Created: demo_project/demo_project/settings.py |
| 53 | + -> The central configuration file. Database settings, installed apps, |
| 54 | + middleware, templates, and more are all configured here. |
| 55 | +... |
| 56 | +``` |
| 57 | + |
| 58 | +Running `python manage.py migrate` applies Django's built-in database tables: |
| 59 | + |
| 60 | +```text |
| 61 | +Operations to perform: |
| 62 | + Apply all migrations: admin, auth, contenttypes, sessions |
| 63 | +Running migrations: |
| 64 | + Applying contenttypes.0001_initial... OK |
| 65 | + Applying auth.0001_initial... OK |
| 66 | + ... |
| 67 | +``` |
| 68 | + |
| 69 | +## Alter it |
| 70 | + |
| 71 | +1. Open `setup_guide.py` and add a new model field to the `Item` model (e.g., `description = models.TextField(blank=True)`). Re-run the script and verify the field appears. |
| 72 | +2. Add a second model called `Category` with a `name` field. Add a `ForeignKey` from `Item` to `Category`. |
| 73 | +3. Modify the script to also generate an `admin.py` that registers both models. |
| 74 | + |
| 75 | +## Break it |
| 76 | + |
| 77 | +1. In the generated `settings.py`, remove `'django.contrib.admin'` from `INSTALLED_APPS`. Try visiting `/admin`. What happens? |
| 78 | +2. In the generated model, change `models.CharField(max_length=200)` to `models.CharField()` (remove max_length). Try running `makemigrations`. What error do you get? |
| 79 | +3. Delete the generated `migrations/` directory and try running `migrate`. What happens? |
| 80 | + |
| 81 | +## Fix it |
| 82 | + |
| 83 | +1. Add `'django.contrib.admin'` back to `INSTALLED_APPS`. The admin interface requires this app. |
| 84 | +2. Add `max_length=200` back. `CharField` requires a `max_length` argument. If you want unlimited text, use `TextField` instead. |
| 85 | +3. Run `makemigrations` first to regenerate the migration files, then `migrate`. Django needs migration files to know what database changes to make. |
| 86 | + |
| 87 | +## Explain it |
| 88 | + |
| 89 | +1. What is the difference between a Django "project" and a Django "app"? Why does Django separate them? |
| 90 | +2. What does `manage.py` do? How is it different from `django-admin`? |
| 91 | +3. What are migrations? Why doesn't Django just read your models and create tables directly? |
| 92 | +4. What does `INSTALLED_APPS` control? What happens if you define a model in an app that is not listed there? |
| 93 | + |
| 94 | +## Mastery check |
| 95 | + |
| 96 | +You can move on when you can: |
| 97 | + |
| 98 | +- explain every file in a new Django project without looking at the guide, |
| 99 | +- create a model, make migrations, and apply them from memory, |
| 100 | +- access the admin interface and add/edit objects, |
| 101 | +- describe what `settings.py` controls and find any setting by name. |
| 102 | + |
| 103 | +## Next |
| 104 | + |
| 105 | +Continue to [02-views-templates](../02-views-templates/). |
0 commit comments