Module 2: Django Framework

Learn Django framework fundamentals including installation, project structure, settings configuration, and URL routing for web development.

Back to Course|6 hours|Intermediate

Django Framework

Learn Django framework fundamentals including installation, project structure, settings configuration, and URL routing for web development.

Progress: 0/5 topics completed0%

Select Topics Overview

Django Installation & Setup

Learn how to install Django, set up a virtual environment, and create a new Django project.

Content by: Manali Trivedi

Python Django Developer

Connect

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.

Installation

Code Example
# 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/
Swipe to see more code

Practice Exercise: Project Setup Script

Code Example
# 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()
Swipe to see more code

šŸŽÆ Practice Exercise

Test your understanding of this topic:

Additional Resources

šŸ“š Recommended Reading

  • •Django Official Documentation
  • •Django for Beginners by William S. Vincent
  • •Two Scoops of Django by Daniel Greenfeld

🌐 Online Resources

  • •Django Tutorial (djangoproject.com)
  • •Django Girls Tutorial
  • •Real Python Django Tutorials

Ready for the Next Module?

Continue your learning journey and master the next set of concepts.

Continue to Module 3