Install Django

Installing Django framework in your virtual environment

⚙️ Installing Django

Install Django using pip package manager inside your activated virtual environment. This ensures Django is installed only for your project, keeping your system Python clean and avoiding version conflicts with other projects.


# Install Django (latest version)
pip install django

# Verify installation
django-admin --version
                                    

Output:

Successfully installed django-5.0.1

5.0.1

Installation Methods

🚀

Latest Version

Install the newest Django release

pip install django
📌

Specific Version

Install a particular Django version

pip install django==4.2
⬆️

Upgrade Django

Update to the latest version

pip install --upgrade django
📋

From Requirements

Install from requirements file

pip install -r requirements.txt

🔹 Step-by-Step Installation

Follow these steps to install Django correctly in your virtual environment. Make sure your virtual environment is activated before running the installation command to ensure Django installs in the project-specific environment.

🔸 Step 1: Activate Virtual Environment

# Windows
venv\Scripts\activate

# Mac/Linux
source venv/bin/activate

# You should see (venv) in your prompt
(venv) C:\myproject>

🔸 Step 2: Upgrade pip (Recommended)

# Ensure you have the latest pip
python -m pip install --upgrade pip

Output:

Successfully installed pip-23.3.1

🔸 Step 3: Install Django

# Install latest Django version
pip install django

# Or install specific version
pip install django==5.0

# Or install LTS (Long Term Support) version
pip install django==4.2

Output:

Collecting django

Downloading Django-5.0.1-py3-none-any.whl (8.2 MB)

Installing collected packages: django

Successfully installed django-5.0.1

🔹 Verifying Django Installation

Confirm Django is installed correctly and check the version:

# Check Django version
django-admin --version

# Alternative method
python -m django --version

# Check installed packages
pip list

# Show Django details
pip show django

Output:

5.0.1

Name: Django

Version: 5.0.1

Summary: A high-level Python web framework

Location: C:\myproject\venv\Lib\site-packages

🔹 Testing Django Installation

Create a test project to ensure Django is working properly:

# Create a test project
django-admin startproject testproject

# Navigate to project
cd testproject

# Run development server
python manage.py runserver

Output:

Watching for file changes with StatReloader

Performing system checks...

System check identified no issues (0 silenced).

Starting development server at http://127.0.0.1:8000/

Quit the server with CTRL-BREAK.

✅ Open your browser and visit http://127.0.0.1:8000/

You should see the Django welcome page with a rocket! 🚀

🔹 Installing Additional Packages

Common packages you might need with Django:

# Database driver for PostgreSQL
pip install psycopg2-binary

# For working with images
pip install pillow

# REST API framework
pip install djangorestframework

# Environment variables
pip install python-decouple

# All at once
pip install django pillow djangorestframework python-decouple

🔹 Creating Requirements File

Save your project dependencies for easy sharing and deployment. The requirements file lists all installed packages with their versions, making it simple for others to recreate your exact development environment.

# Generate requirements.txt
pip freeze > requirements.txt

# View the file
cat requirements.txt  # Mac/Linux
type requirements.txt # Windows

requirements.txt content:

asgiref==3.7.2

Django==5.0.1

sqlparse==0.4.4

tzdata==2023.3

# Install from requirements.txt (on another machine)
pip install -r requirements.txt

🔹 Django Version Information

Understanding Django versions:

Django Version Types:

  • LTS (Long Term Support): Django 4.2 - Supported until April 2026
  • Latest Stable: Django 5.0 - Current release with newest features
  • Development: Django 5.1 - Upcoming version (not for production)

Choosing a Version:

  • For Learning: Use latest stable (Django 5.0)
  • For Production: Use LTS version (Django 4.2)
  • For Existing Projects: Match the project's version

🔹 Troubleshooting Installation

Common installation issues and solutions:

Problem: "pip: command not found"

Solution: Use python -m pip install django instead

Problem: Permission denied error

Solution: Make sure virtual environment is activated. Don't use sudo!

Problem: Django installs but django-admin not found

Solution: Deactivate and reactivate virtual environment

Problem: Old Django version installed

Solution: Run pip install --upgrade django

Problem: Installation is very slow

Solution: Check internet connection or use pip install django --no-cache-dir

🔹 Uninstalling Django

If you need to remove Django:

# Uninstall Django
pip uninstall django

# Confirm when prompted
# Type 'y' and press Enter

# Verify removal
pip list | grep -i django  # Mac/Linux
pip list | findstr django  # Windows

🧠 Test Your Knowledge

What command installs Django in your virtual environment?