Django Installation & Setup
Learn how to install Django, set up a virtual environment, and create a new Django project. This is a foundational concept in Python web development that professional developers rely on daily. The explanations below are written to be beginner-friendly while covering the depth and nuance that comes from real-world Python/Django experience. Take your time with each section and practice the examples
What is Django?
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It follows the Model-View-Template (MVT) pattern and includes many built-in features.. This is an essential concept that every Python/Django developer must understand thoroughly. In professional development environments, getting this right can mean the difference between code that works reliably and code that breaks in production. The following sections break this down into clear, digestible pieces with practical examples you can try immediately
Installation
# Install Django using pip
pip install django
# Check Django version
python -m django --version
# Create a virtual environment (recommended)
python -m venv myenv
# Activate virtual environment
# On Windows:
myenv\Scripts\activate
# On macOS/Linux:
source myenv/bin/activate
# Install Django in virtual environment
pip install django
# Create a new Django project
django-admin startproject myproject
# Navigate to project directory
cd myproject
# Run the development server
python manage.py runserver
# Access your site at http://127.0.0.1:8000/Practice Exercise: Project Setup Script
# Django Project Setup Script
import os
import subprocess
import sys
def create_django_project():
"""Automated Django project setup"""
project_name = input("Enter project name: ")
# Create virtual environment
print(f"Creating virtual environment for {project_name}...")
subprocess.run([sys.executable, "-m", "venv", f"{project_name}_env"])
# Activate virtual environment and install Django
if os.name == 'nt': # Windows
activate_script = f"{project_name}_env\Scripts\activate"
pip_path = f"{project_name}_env\Scripts\pip"
else: # macOS/Linux
activate_script = f"{project_name}_env/bin/activate"
pip_path = f"{project_name}_env/bin/pip"
print("Installing Django...")
subprocess.run([pip_path, "install", "django"])
# Create Django project
print(f"Creating Django project: {project_name}")
subprocess.run([pip_path, "run", "django-admin", "startproject", project_name])
print(f"\nProject {project_name} created successfully!")
print(f"To get started:")
print(f"1. cd {project_name}")
print(f"2. Activate virtual environment: {activate_script}")
print(f"3. Run: python manage.py runserver")
if __name__ == "__main__":
create_django_project()