Installing Django & Virtual Environment Setup
Before writing Django code, you need Python 3.8+, a virtual environment to isolate your project dependencies, and Django itself installed via pip. This topic walks you through the complete setup process.
Why Virtual Environments?
A virtual environment creates an isolated Python installation for each project. This prevents package conflicts between projects — one project might need Django 4.2 while another needs Django 5.0. Without virtual environments, installing a new version would break the old project.
QuerySets are LAZY — no DB hit until evaluated.
Setup Steps
- Ensure Python 3.8+ is installed: python --version
- Create a project folder: mkdir myproject && cd myproject
- Create virtual environment: python -m venv venv
- Activate it — Windows: venv\\Scripts\\activate
- Activate it — macOS/Linux: source venv/bin/activate
- Install Django: pip install django
- Verify: django-admin --version
- Freeze dependencies: pip freeze > requirements.txt
Installation Commands
# Step 1: Create project directory
# mkdir myproject
# cd myproject
# Step 2: Create virtual environment
# python -m venv venv
# Step 3: Activate virtual environment
# Windows: venv\Scripts\activate
# macOS/Linux: source venv/bin/activate
# Step 4: Install Django
# pip install django
# Step 5: Verify installation
# django-admin --version
# Output: 5.1 (or latest version)
# Step 6: Save dependencies
# pip freeze > requirements.txtTip
Tip
Each Django app should do ONE thing. A 'blog' app handles posts, a 'users' app handles auth. This makes apps reusable across projects.
Common Mistake
Warning
Forgetting to add your app to INSTALLED_APPS in settings.py. Without this, Django won't discover your models, views, or templates.
Practice Task
Note
(1) Create a new app: python manage.py startapp blog. (2) Add 'blog' to INSTALLED_APPS. (3) Explore the generated file structure.
Quick Quiz
Key Takeaways
- Before writing Django code, you need Python 3.
- Ensure Python 3.8+ is installed: python --version
- Create a project folder: mkdir myproject && cd myproject
- Create virtual environment: python -m venv venv