Installing Django and starting a project
1. Create a virtual environment
A virtual environment keeps each project's packages separate. In a terminal:
Terminal
mkdir mysite
cd mysite
python -m venv .venvActivate it:
Terminal
# Windows (PowerShell)
.venv\Scripts\Activate.ps1
# macOS / Linux
source .venv/bin/activateYour prompt now starts with (.venv).
2. Install Django
Terminal
python -m pip install django
python -m django --version3. Start a project
Terminal
django-admin startproject config .The . at the end creates the project in the current folder. You get:
Diagram
mysite/
├── manage.py ← run commands for this project
└── config/
├── __init__.py
├── settings.py ← all configuration
├── urls.py ← the main URL table
├── asgi.py ← entry points for deployment servers
└── wsgi.pyNaming the project package config makes its job obvious; many teams use that convention.
4. Run the development server
Terminal
python manage.py runserverOutput
Watching for file changes with StatReloader
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.Open http://127.0.0.1:8000/ and you'll see Django's welcome page with a rocket. The server reloads automatically when you change code.
5. Set up the database
Django uses SQLite by default, which needs no setup. Create its built-in tables (for users, sessions and the admin):
Terminal
python manage.py migrateImportant settings
A few lines in config/settings.py you'll touch early:
Python
DEBUG = True # detailed error pages: never True in production
ALLOWED_HOSTS = [] # domains allowed to serve the site, e.g. ["codemirai.com"]
INSTALLED_APPS = [...] # the apps that make up the project
TIME_ZONE = "Asia/Kolkata" # your time zoneFinished this lesson?Mark it complete to earn +20 XP and move your progress on.